GCC 4.8在与libmagic链接时失败

时间:2015-09-03 13:49:08

标签: c++ c gcc libmagic

在将我的代码与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的示例代码here。我已经检查过,这个问题也出现了。显然libmagic和libmagic-dev安装在我的系统上(Ubuntu 14.04)。

有没有办法处理这个问题,不同的是降级gcc版本?

1 个答案:

答案 0 :(得分:1)

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

我不认为您的编辑成功gcc-4.3

gcc的论据顺序很重要(参见例如this);目标文件和库应该是最后的(从高级到低级)。试试

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

顺便说一句,请不要调用您的可执行文件test(例如mytest),因为test通常是内置的shell。