【问题标题】:GCC 4.8 fails while linking with libmagicGCC 4.8 在与 libmagic 链接时失败
【发布时间】:2015-11-29 09:35:39
【问题描述】:

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

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

除了降级 gcc 版本之外,有什么方法可以处理这个问题?

【问题讨论】:

  • 我投了反对票,因为您的问题没有显示足够的研究。通过网络搜索,您将获得无数答案。

标签: c++ c gcc libmagic


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2022-07-29
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2015-01-02
    • 1970-01-01
    • 2015-07-27
    • 2014-02-09
    相关资源
    最近更新 更多