如何使用 boost.python 中的 -fPIC 编译静态库

2021-12-24 00:00:00 python c c++ boost boost-python

默认情况下,libboostpython.a 是在没有 -fPIC 的情况下编译的.但我必须制作一个 python 扩展,它是一个动态库,带有 -fPIC 链接到静态库.如何使用 boost.python 中的 -fPIC 编译静态库 (libboostpython.a)?

By default, libboostpython.a is compiled without -fPIC. But I have to make a python extension and it is a dynamic library with -fPIC that links to static libraries. How can I compile a static library (libboostpython.a) with -fPIC from boost.python?

推荐答案

您可以使用以下几个选项:

There are a couple options you could use:

  • 从源代码编译 boost 并将额外的编译器选项传递给 bjam.例如.bjam ... cxxflags='-fPIC'.这会将每个 boost 源文件编译为位置无关代码.
  • 以共享库的形式使用boost.在这种情况下,您可能希望随应用程序一起提供 boost 共享库,以确保使用适当版本的 boost.您可以使用 '-Wl,-rpath,$ORIGIN' 标志链接您的可执行文件,这样当动态链接器搜索您的可执行文件所需的共享库时,它会在可执行文件所在的目录中查找它们.请参阅man ld.so有关 $ORIGIN 的更多详细信息.
  • Compile boost from source and pass extra compiler options to bjam. E.g. bjam ... cxxflags='-fPIC'. That would compile every boost source file as position independent code.
  • Use boost in the form of shared libraries. In this case you probably want to ship boost shared libraries along with your application to make sure the appropriate version of boost is used. You can link your executable with '-Wl,-rpath,$ORIGIN' flag, so that when the dynamic linker searches for shared libraries required by your executable it looks for them in the directory where the executable is. See man ld.so for more details on $ORIGIN.

相关文章