编译使用glib时出现链接错误...?

10
我在Ubuntu上编译一个简单的glib示例程序时遇到了问题。我得到了以下错误。我可以使用-c标志进行编译,但无法进行链接,这意味着我已经安装了glib头文件,但没有找到共享对象代码。请参阅下面的Make文件。
$> make re
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include  -lglib-2.0       re.c   -o re
/tmp/ccxas1nI.o: In function `print_uppercase_words':
re.c:(.text+0x21): undefined reference to `g_regex_new'
re.c:(.text+0x41): undefined reference to `g_regex_match'
re.c:(.text+0x54): undefined reference to `g_match_info_fetch'
re.c:(.text+0x6e): undefined reference to `g_print'
re.c:(.text+0x7a): undefined reference to `g_free'
re.c:(.text+0x8b): undefined reference to `g_match_info_next'
re.c:(.text+0x97): undefined reference to `g_match_info_matches'
re.c:(.text+0xa7): undefined reference to `g_match_info_free'
re.c:(.text+0xb3): undefined reference to `g_regex_unref'
collect2: ld returned 1 exit status
make: *** [re] Error 1

使用的Makefile:

# Need to installed libglib2.0-dev some system specific install that will
# provide a value for pkg-config
INCLUDES=$(shell pkg-config --libs --cflags glib-2.0)
CC=gcc $(INCLUDES)
PROJECT=re

# Targets
full: clean compile

clean:
    rm $(PROJECT)

compile:
    $(CC) $(PROJECT).c -o $(PROJECT)

.c 代码正在被编译:

#include <glib.h>

void print_upppercase_words(const gchar *string)
{
  /* Print all uppercase-only words. */

  GRegex *regex;
  GMatchInfo *match_info;

  regex = g_regex_new("[A-Z]+", 0, 0, NULL);
  g_regex_match(regex, string, 0, &match_info);

  while (g_match_info_matches(match_info))
    {
      gchar *word = g_match_info_fetch(match_info, 0);
      g_print("Found %s\n", word);
      g_free(word);
      g_match_info_next(match_info, NULL);
    }

  g_match_info_free(match_info);
  g_regex_unref(regex);
}

int main()
{
  gchar *string = "My body is a cage.  My mind is THE key.";

  print_uppercase_words(string);
}

奇怪的是,当我运行glib-config时,它不喜欢那个命令,尽管我不知道如何告诉Bash或Make在抱怨gdlib-config存在于这两个软件包时如何只使用其中一个。

$> glib-config
No command 'glib-config' found, did you mean:
 Command 'gdlib-config' from package 'libgd2-xpm-dev' (main)
 Command 'gdlib-config' from package 'libgd2-noxpm-dev' (main)
glib-config: command not found

那么解决方案是什么,我不明白? - FreelanceConsultant
2个回答

29

编译器命令末尾的库:

gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include re.c -o re -lglib-2.0

来自GCC链接选项:

-llibrary
-l library
    链接时搜索名为library的库。
    (将库作为单独参数的第二种选择只是为了符合POSIX标准,不建议使用。)
写这个选项的位置很重要;链接器按照指定的顺序搜索和处理库和目标文件。 因此 "foo.o -lz bar.o" 在文件foo.o之后但在bar.o之前搜索库"z"。 如果bar.o引用了“z”中的函数,则可能无法加载这些函数。

1
这就是所谓的单遍链接:https://computation.llnl.gov/casc/components/docs/users_guide/node319.html - John Zwinck
我在研究这个问题时读到了这篇文章,并尝试了以下所有方法:gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0 re.c -o regcc -lglib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include re.c -o regcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include re.c -o re -lglib-2.0我认为顺序并不会改变任何东西。我认为-lglib-2.0标志应该指向.so文件,但我不认为它找到了.so文件。 - lucidquiet
1
那么解决方案是什么,我不明白? - FreelanceConsultant
1
为了明确起见,必须将所有“-l”参数附加到编译命令的末尾。这意味着"pkg-config --libs glib-2.0"(生成小写L参数)应该是命令中的最后一个,而"pkg-config --cflags glib-2.0"(生成大写I参数)应该出现在“.c”源文件之前 - Rui Pimentel
对于在Codeblocks(Code :: Blocks)环境中遇到此问题的人,您只需要在“编译器设置>>其他选项”下放置“pkg-config --cflags glib-2.0”,并在“链接器设置>>其他链接器选项”下放置“pkg-config --libs glib-2.0”即可。请注意,这些设置可能适用于整个项目或目标配置文件之一(“调试”/“发布”/等),具体取决于左侧面板上选择了什么。此外,如果其他人在使用GTK+时遇到类似问题,则可以将“glib-2.0”替换为“gtk+-3.0”。 - Rui Pimentel

3
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include re.c -o re -lglib-2.0

哦,天啊!我尝试了很多方法,可能都有点不对,但是我刚刚尝试了上面的方法,它起作用了...所以问题解决了。


这就是为什么我接受了你的答案。我想我应该只是在你的帖子下添加一个评论。 - lucidquiet
您需要点击答案旁边的白色勾号来接受答案。 - hmjd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接