CMake:使用静态库的共享库
我正在尝试创建一个链接静态库的共享库(实际上是一个Python模块)。这两个库都是同一项目的一部分,并使用 cmake 构建。
现在,共享库是这样构建的:
add_library(MyLibPython SHARED ${PYTHON_WRAPPERS_SRC})
set_target_properties(MyLibPython PROPERTIES PREFIX "")
target_link_libraries(MyLibPython MyLibStatic ${LIBS})
这构建时没有错误,但是当我尝试导入 Python 模块时,我得到:
ImportError: lib/python/MyLibPython.so: undefined symbol: _Zone_of_my_MyLibStatic_functions
我还有许多以类似方式构建的可执行文件(单元测试),并且它们可以工作完美。
我应该补充一下,这是在 Linux 上使用 gcc。
I am trying to create a shared library (really a Python module) that links against a static library. Both libraries are part of the same project and built using cmake.
Now, the shared library is built like this:
add_library(MyLibPython SHARED ${PYTHON_WRAPPERS_SRC})
set_target_properties(MyLibPython PROPERTIES PREFIX "")
target_link_libraries(MyLibPython MyLibStatic ${LIBS})
This builds without error, but when I try to import the Python module, I get:
ImportError: lib/python/MyLibPython.so: undefined symbol: _Zone_of_my_MyLibStatic_functions
I also have a number of executables (unit tests) that are built in a similar way, and they work perfectly.
I should add, this is using gcc on Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查您的链接器命令行。它是否传递了类似
-Wl,--as-needed
的内容?如果是这样,它可能不包含静态库所需的所有内容。我不认为你的技术一般来说是可移植的。你能得到一个共享库来链接吗?我认为在某些平台上,进入共享库的所有内容都需要编译为 PIC。
无论如何,要使用 GNU ld 链接整个档案(查找
man ld
):Check your linker command line. Is it passing something like
-Wl,--as-needed
? If so, it might not be including everything required by the static library.I don't think your technique is portable in general. Can you get a shared library to link against? I think that there are some platforms where everything that goes into a shared library needs to be compiled as PIC.
Anyway, to link an entire archive with GNU ld (look up
man ld
):