【问题标题】:Error compiling with GLib使用 GLib 编译时出错
【发布时间】:2015-03-20 18:20:24
【问题描述】:

我对 C 编程非常陌生,并且正在尝试完成“21 世纪 C”第二版中的练习。我被困在第 202 页,示例 9-7,unicode.c。这个例子开始于:

#include <glib.h>
#include <locale.h> //setlocale
#include "string_utilities.h"
#include "stopif.h"

//Frees instring for you--we can't use it for anything else.
char *localstring_to_utf8(char *instring){
    GError *e=NULL;
    setlocale(LC_ALL, ""); //get the OS's locale.
    char *out = g_locale_to_utf8(instring, -1, NULL, NULL, &e);
    free(instring); //done with the original
    Stopif(!out, return NULL, "Trouble converting from your locale to UTF-8.");
    Stopif(!g_utf8_validate(out, -1, NULL), free(out); return NULL,
            "Trouble: I couldn't convert your file to a valid UTF-8 string.");
    return out;
}

当我尝试编译它时:

c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o   -o unicode

我收到如下错误:

$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o   -o unicode
/tmp/ccBDQFiH.o: In function `localstring_to_utf8':
/home/kevin/21st_Century_C/ch09/unicode.c:29: undefined reference to `g_locale_to_utf8'
/home/kevin/21st_Century_C/ch09/unicode.c:32: undefined reference to `g_utf8_validate'
/tmp/ccBDQFiH.o: In function `main':
/home/kevin/21st_Century_C/ch09/unicode.c:48: undefined reference to `g_utf8_strlen'

这似乎表明找不到 Glib 库,但编译器并没有抱怨这一点,而且 Glib 库和包含文件就在我在命令行中指定的位置。除了 libglib2.0 包之外,我还安装了 libglib2.0-dev 包(全部使用 'sudo apt-get ..' 安装)。 'pkg-config' 似乎觉得 glib-2.0 很好。

这一切都在 Ubuntu 14.04.2 系统上。

我不知道如何纠正这个错误,也不明白为什么它找不到特定的 Glib 函数,如果它找到了 glib 包含和 lib 文件。

【问题讨论】:

  • 在目标文件之后指定-lglib-2.0 怎么样?
  • @CamiloBravoValdes:不高兴:'kevin@kevin-asrock:~/21st_Century_C/ch09$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu /glib-2.0/include -g -Wall -O3 -lglib-2.0 unicode.c string_utilities.o -lglib-2.0。 -o unicode /usr/bin/ld: 找不到 -lglib-2.0。 collect2: 错误: ld 返回 1 退出状态 kevin@kevin-asrock:~/21st_Century_C/ch09$ c99 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Wall -O3 unicode.c string_utilities.o -lglib-2.0。 -o unicode /usr/bin/ld: 找不到 -lglib-2.0。 collect2:错误:ld 返回 1 个退出状态 $`
  • @SeanBright: 还是不开心:cc pkg-config --cflags glib-2.0` -g -Wall -O3 -std=gnu11 pkg-config --libs glib-2.0unicode.c string_utilities.o -o unicode ` 给出相同的错误
  • 如果你在-L/path/to/glib中包含图书馆的位置?
  • @CamiloBravoValdés:pkg-config 不返回任何内容:$ pkg-config --libs-only-L glib-2.0 kevin@kevin-asrock:~/21st_Century_C/ch09$

标签: c compilation libraries glib


【解决方案1】:

命令行中事物的顺序很重要。一般来说,它应该是这样的:

gcc [options] [source files] [object files] [-L stuff] [-lstuff] [-o outputfile]

所以试一试吧:

gcc -g -Wall -O3 -std=gnu11 `pkg-config --cflags glib-2.0` \
    unicode.c string_utilities.o `pkg-config --libs glib-2.0` \
    -o unicode

这也包含在GLib Reference ManualCompiling GLib Applications 部分中:

$ cc hello.c `pkg-config --cflags --libs glib-2.0` -o hello

【讨论】:

  • 再次感谢。现在,我想知道如何将其放入 make 文件中。作者建议:P=unicode objects=string_utilities.o CFLAGS=pkg-config --cflags glib-2.0` -g -Wall -std=gnu11 -O3 LDLIBS=pkg-config --libs glib-2.0 $(P): $(objects) ' 但这并没有把库在正确的地方。 LDADD 似乎根本不起作用,并且没有将库放在编译器命令中的任何地方。有什么办法可以把它们按正确的顺序排列?
  • 我收回了。这个 makefile 工作得很好:P=unicode objects=string_utilities.o CFLAGS=pkg-config --cflags glib-2.0` -g -Wall -O3 -std=gnu11 LDLIBS=pkg-config --libs glib-2.0 $(P): $(objects) ` 不知道我是什么在想……
猜你喜欢
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
相关资源
最近更新 更多