【问题标题】:Linker errors when compiling against glib...?针对 glib 编译时出现链接器错误...?
【发布时间】:2012-04-15 13:18:19
【问题描述】:

我无法在 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 时,它不喜欢那个命令,尽管当它抱怨 gdlib-config 在这些命令中时,我不知道如何告诉 Bash 或 Make 如何只使用一个而不是另一个两个包。

$> 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

【问题讨论】:

  • 那么解决方案是什么,我不明白?

标签: c compilation linker


【解决方案1】:

编译器命令末尾的库:

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 Link Options

-图书馆 -l 库 链接时搜索名为 library 的库。 (将库作为单独参数的第二种选择 仅用于 POSIX 合规性,不推荐使用。) 在命令中编写此选项的位置有所不同; 链接器在 它们被指定的顺序。 因此,`foo.o -lz bar.o' 在文件 foo.o 之后搜索库 `z' 但是 bar.o 之前如果 bar.o 引用 `z' 中的函数,那些函数 可能无法加载。

【讨论】:

  • 好吧,我在研究这个问题时读到过,并且我已经尝试了所有这些: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 gcc -lglib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/包括 re.c -o re 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 我不认为订单实际上正在改变任何东西。我认为 -lglib-2.0 标志应该指向 .so 文件——但我认为它没有找到 .so 文件。
  • 那么解决方案是什么,我不明白?
  • 为了清楚起见,有必要将所有“-l”参数附加到编译命令的end。这意味着“pkg-config --libs glib-2.0”(生成 小写 L 参数)应该在命令的最后,而“pkg-config --cflags glib-2.0”(生成 大写 I 参数) ) 应该出现在 ".c" 源文件之前。
  • 对于在 Codeblocks (Code::Blocks) 环境中遇到此问题的人,您只需将pkg-config --cflags glib-2.0 放在“项目构建”的“编译器设置>>其他选项”下即可选项”对话框和“链接器设置>>其他链接器选项”下的pkg-config --libs glib-2.0。请注意,这些设置可能适用于整个项目或目标配置文件之一(“调试”/“发布”等),具体取决于左侧面板上选择的内容。此外,如果其他人在使用 GTK+ 时遇到类似问题,可以将“glib-2.0”替换为“gtk+-3.0”。
【解决方案2】:
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

呃——见鬼——我已经尝试了很多东西,可能都弄错了,但我刚刚尝试了上面的方法,它奏效了....解决了。

【讨论】:

  • 是的——这就是我接受你的回答的原因。我想我应该在您的帖子中添加评论。
  • 您必须点击答案旁边的白色勾号才能接受。
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
相关资源
最近更新 更多