使用gcc -l后缺少Ldd中的库 [英] Missing a library in ldd after using gcc -l

查看:377
本文介绍了使用gcc -l后缺少Ldd中的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下命令编译一个名为"interrogate"的可执行文件:

I'm compiling an executable called "interrogate" with:


g++ -o built/bin/interrogate -Lbuilt/lib -Lbuilt/tmp -L/usr/X11R6/lib \
built/tmp/interrogate_composite1.o built/tmp/interrogate_composite2.o \
-lp3cppParser -lp3dtool -lp3dtoolconfig -lp3pystub -pthread -ldl

编译后,当我尝试执行可执行文件时:

After the compilation, when i try to execute the executable:


$ LD_LIBRARY_PATH=built/lib built/bin/interrogate
built/bin/interrogate: symbol lookup error: built/lib/libp3dtool.so.1.8: undefined symbol: _Py_NoneStruct

此符号由libp3pystub.so提供,但是查询可执行文件对此库没有任何引用(我确实使用过-lp3pystub):

This symbol is provided by the libp3pystub.so, but the interrogate executable doesn't have any reference to this library (I did used the -lp3pystub):


$ LD_LIBRARY_PATH=built/lib ldd built/bin/interrogate
    linux-vdso.so.1 =>  (0x00007fff2016a000)
    libp3dtool.so.1.8 => built/lib/libp3dtool.so.1.8 (0x00007f498d57a000)
    libp3dtoolconfig.so.1.8 => built/lib/libp3dtoolconfig.so.1.8 (0x00007f498d51b000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f498d1f2000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f498cfdc000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f498cdbf000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f498c9ff000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f498c7fb000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f498c4ff000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f498d5bc000)

如果我手动加载libp3pystub.so,则可执行文件有效:

If i manually load the libp3pystub.so, the executable works:


$ LD_PRELOAD=built/lib/libp3pystub.so LD_LIBRARY_PATH=built/lib built/bin/interrogate

Usage:
  interrogate [opts] file.C [file.C ...]
  interrogate -h

我的问题是:为什么interrogate可执行文件未引用我用-lp3pystub添加的库?

My question is: why the library i've added with -lp3pystub is not referenced by the interrogate executable?

推荐答案

我实际上自己找到了答案.我在Ubuntu上进行编译,并且它们添加了默认的优化标志:-Wl,--as-needed.此优化检查是否在主可执行文件中未使用-l传递的库中的符号,将其删除.

I actually find the answer myself. I was compiling on Ubuntu, and they added a default optimization flags: -Wl,--as-needed. This optimization check if no symbol from the libraries passed with -l are used in the main executable, they will be removed.

那是我的错误所在:_Py_NoneStruct不是直接由询问使用,而是由另一个共享库使用.因此,我必须手动指定需要p3pystub.

And that's where my error was: _Py_NoneStruct is not directly used by interrogate, but by another shared library. So i must manually specify that the p3pystub is needed.

一种可能的解决方法是:

One possible fix would be:


$ g++ -o built/bin/interrogate -Lbuilt/lib -Lbuilt/tmp -L/usr/X11R6/lib \
built/tmp/interrogate_composite1.o built/tmp/interrogate_composite2.o \
-Wl,--no-as-needed -lp3cppParser -lp3dtool -lp3dtoolconfig -lp3pystub \
-pthread -ldl

然后我正确地在ldd输出中获得了该库:

And then i correctly got the library in the ldd output:

$ LD_LIBRARY_PATH=built/lib ldd built/bin/interrogate
    linux-vdso.so.1 =>  (0x00007fff0edff000)
    libp3dtool.so.1.8 => built/lib/libp3dtool.so.1.8 (0x00007fa1c36be000)
    libp3dtoolconfig.so.1.8 => built/lib/libp3dtoolconfig.so.1.8 (0x00007fa1c365f000)
>>> libp3pystub.so.1.8 => built/lib/libp3pystub.so.1.8 (0x00007fa1c3658000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa1c342f000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa1c312c000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa1c2e2f000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa1c2c19000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa1c29fc000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa1c263c000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fa1c3700000)

参考: https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition

这篇关于使用gcc -l后缺少Ldd中的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆