GCC 4.8在链接libmagic时失败

GCC 4.8 fails while linking with libmagic

本文关键字:libmagic 失败 链接 GCC      更新时间:2023-10-16

我正在努力解决这个问题,同时链接我的代码对libmagic:

test.c:(.text+0x16): undefined reference to `magic_open'
test.c:(.text+0x50): undefined reference to `magic_load'
test.c:(.text+0x60): undefined reference to `magic_error'
test.c:(.text+0x84): undefined reference to `magic_close'
test.c:(.text+0x9e): undefined reference to `magic_file'
test.c:(.text+0xba): undefined reference to `magic_close'
collect2: ld returned 1 exit status

然而,这个问题只出现在gcc版本> 4.4时。为了进行编译,我使用以下命令:

gcc -L/usr/lib/ -lmagic  test.c -o test

使用libmagic的示例代码可以在这里找到。我检查过了,这个问题也出现了。显然libmagic和libmagic-dev已经安装在我的系统上了(Ubuntu 14.04)。

是否有任何方法来处理这个问题,而不是降级gcc版本?

这是一个常见问题解答,与您的GCC版本无关。

我不认为你的编译成功的gcc-4.3

gcc的参数顺序很重要(例如this);对象文件和库应该放在最后(从高级到低级)。试着与

 gcc  -Wall -g test.c -lmagic  -o mytest

顺便说一句,不要把你的可执行文件命名为test(但例如mytest),因为test通常是shell内置的