0

在将我的代码与 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 版本?

4

1 回答 1

1

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

我认为您的编译没有成功gcc-4.3

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

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

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

于 2015-09-03T13:51:48.627 回答