C 针对glib进行编译时出现链接器错误。。。?

C 针对glib进行编译时出现链接器错误。。。?,c,compilation,linker,C,Compilation,Linker,我在Ubuntu上编译一个简单的、针对glib的示例程序时遇到了问题。我得到以下错误。我可以让它编译,但不能链接到-c标志,我相信这意味着我安装了glib头,但它找不到共享的目标代码。另请参见下面的文件 $> 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 `

我在Ubuntu上编译一个简单的、针对glib的示例程序时遇到了问题。我得到以下错误。我可以让它编译,但不能链接到
-c
标志,我相信这意味着我安装了glib头,但它找不到共享的目标代码。另请参见下面的文件

$> 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);
}

编译器命令末尾的库:

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

发件人:

-图书馆 -图书馆 链接时搜索名为library的库。 (第二种选择是将库作为单独的参数 仅适用于POSIX合规性,不建议使用。) 在命令中写入此选项的位置会有所不同; 链接器在中搜索和处理库和对象文件 指定它们的顺序。 因此,`foo.o-lzbar.o'在文件foo.o之后搜索库`z',但是 在酒吧前。如果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/lib/x86_/x86_64-linux-64-linux-gnu/include-re.c-o-re-gcc-I/usr/include/glib-2.0-2.0-l/glib-2.0-2.0-I/usr/glib/glib-2.0秩序实际上改变了一切。我认为-lglib-2.0标志应该指向.so文件——但我认为它没有找到.so文件。所以,我不明白解决方案是什么?只是为了说明,所有“-l”参数都必须附加到编译命令的末尾。这意味着“
pkg-config--libs glib-2.0
”(生成小写L参数)应该是命令中的最后一个,而“
pkg-config--cflags glib-2.0
”(生成大写I参数)应该出现在“.c”源文件之前环境中,只需将
pkg配置--cflags glib-2.0
放在“项目构建选项”对话框的“编译器设置>>其他选项”下,并将
pkg配置--libs glib-2.0
放在“链接器设置>>其他链接器选项”下。请注意,这些设置可能会应用于整个项目或其中一个目标配置文件(“调试”/“发布”/等),具体取决于左侧面板上选择的内容。另外,如果其他人在GTK+上遇到类似的问题,可以将“glib-2.0”替换为“GTK+-3.0”。这就是为什么我接受了你的答案。我想我应该在你的帖子中添加一条评论。你必须点击答案旁边的白色勾号才能接受。那么解决方案是什么,我不明白?
$> 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
-llibrary -l library Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.) It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.
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