我使用 boost::python 为我的应用程序创建了一个包装器。
到目前为止,这已经通过:(静态库/源代码的数量)-> python_mapping.so
这样,我的共享对象由许多静态库组成,包括 boost 本身(特别是 boost_thread)。我会假设这将包含我所有的应用程序信息,因为我已经静态链接了所有内容。
这编译得很好。
ldd python_mapping.so
librt.so.1 => /lib64/librt.so.1 (0x00002b7cbad37000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00002b7cbaf40000)
libm.so.6 => /lib64/libm.so.6 (0x00002b7cbb240000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002b7cbb4c4000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b7cbb6d2000)
libc.so.6 => /lib64/libc.so.6 (0x00002b7cbb8ed000)
/lib64/ld-linux-x86-64.so.2 (0x000000327ee00000)
但是,当我运行我的示例 python 应用程序时,我得到这个运行时链接错误:未定义的符号:_ZTIN5boost6detail16thread_data_baseE
似乎那些链接到静态库的 boost 库实际上并不存在?
我在这方面取得了一些进展。显然,我的共享对象没有包含很多编译器认为没有被使用的符号(因为它从未见过我的 c++ 对象被创建,因为它们是通过实例化 Python 对象创建的)。
有一些像:
//This is a class only created in python
#include "CPlusPlusClass.h"
PythonClass
{
public:
PythonClass() { }
private:
CPlusPlusClass _cplusplus;
};
//PythonMappings for PythonClass
#python file
import python_mapping
pythonClass = python_mapping.PythonClass() #This fails saying it can't find the symbol for CPlusPlusClass
编译器将优化掉 CPlusCPlus 类,因为它从未看到它实际被使用,这完全令人讨厌。它似乎确实保留了 PythonClass 本身(可能是因为 Python Boost 映射宏。
您可以通过以下几种方式解决此问题:
- 将所有库链接到:
-Xlinker --whole-archive
- 在共享对象中创建库的虚拟使用
我想知道是否有人能想到另一种解决方案,因为浏览所有可能的库并将它们添加到 --whole-archive 真的很烦人。