我对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.0dev包(都安装了'sudo apt.‘)。“‘pkg config”似乎可以找到glib-2.0。
这都是在Ubuntu14.04.2系统上完成的。
我不知道如何纠正这个错误,也不明白如果它找到Glib包含和lib文件,它为什么找不到特定的glib函数。
发布于 2015-03-20 18:43:56
命令行中的顺序很重要。一般来说,应该是这样的:
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应用程序的GLib参考手册部分中也有介绍。
$ cc hello.c `pkg-config --cflags --libs glib-2.0` -o hellohttps://stackoverflow.com/questions/29173306
复制相似问题