CMAKE将动态库链接到模块,但不显示为链接依赖
Posted
技术标签:
【中文标题】CMAKE将动态库链接到模块,但不显示为链接依赖【英文标题】:CMAKE Linking dynamic library to module, but not showing as link dependency 【发布时间】:2017-07-25 18:03:40 【问题描述】:如果以前有人问过,请指出正确的方向。我有 lib1 和 mod2,它们必须链接在一起。这个项目分布在几个文件夹和几个CMakeLists.txt
文件中。我使用的cmake
命令如下:
cmake 文件 1(基本目录):
# Set C/C++ compile and linking flags
set(GCC_COVERAGE_COMPILE_FLAGS "-fpic -Wno-as-needed")
set(GXX_COVERAGE_COMPILE_FLAGS "-std=c++11")
set(GXX_COVERAGE_LINK_FLAGS "-Wl,--no-undefined -Wno-as-needed")
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $GCC_COVERAGE_COMPILE_FLAGS $GXX_COVERAGE_COMPILE_FLAGS")
set(CMAKE_C_FLAGS "$CMAKE_C_FLAGS $GCC_COVERAGE_COMPILE_FLAGS")
set(CMAKE_SHARED_LINKER_FLAGS "$CMAKE_SHARED_LINKER__FLAGS $GCC_COVERAGE_LINK_FLAGS")
cmake 文件 2(lib1 目录):
pybind11_add_module(elka_comm__common
SHARED
pyelka_common.cpp
elka.cpp
elka_comm.cpp
)
set_target_properties(elka_comm__common PROPERTIES
LIBRARY_OUTPUT_DIRECTORY $ELKA_BINARY_DIR/python
)
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $GCC_COVERAGE_COMPILE_FLAGS $GXX_COVERAGE_COMPILE_FLAGS")
set(CMAKE_C_FLAGS "$CMAKE_C_FLAGS $GCC_COVERAGE_COMPILE_FLAGS")
set(CMAKE_SHARED_LINKER_FLAGS "$CMAKE_SHARED_LINKER__FLAGS $GCC_COVERAGE_LINK_FLAGS")
add_dependencies(elka_comm__common msg_gen)
cmake 文件 3(mod2 目录):
#FIXME ldd not showing elka_comm__common as a link dependency
# -> CommPort undefined symbol upon module import
target_link_libraries(
elka_comm__gnd_station
PUBLIC
elka_comm__common
)
set_target_properties(elka_comm__gnd_station PROPERTIES
LIBRARY_OUTPUT_DIRECTORY $ELKA_BINARY_DIR/python
)
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $GCC_COVERAGE_COMPILE_FLAGS $GXX_COVERAGE_COMPILE_FLAGS")
set(CMAKE_C_FLAGS "$CMAKE_C_FLAGS $GCC_COVERAGE_COMPILE_FLAGS")
set(CMAKE_MODULE_LINKER_FLAGS "$CMAKE_MODULE_LINKER__FLAGS $GCC_COVERAGE_LINK_FLAGS")
add_dependencies(elka_comm__gnd_station
elka_comm__common
msg_gen
)
我的一些步骤作为健全性检查是多余的(例如,使用 CMAKE 变量设置标志)。
以下是ldd -r <path-to-mod2.so>/mod2.so
的部分输出:
linux-vdso.so.1 => (0x00007fff777fe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fadfe690000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fadfe479000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fadfe0b0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fadfddaa000)
/lib64/ld-linux-x86-64.so.2 (0x000055f1e6b2c000)
undefined symbol: _ZTIN4elka8CommPortE (build_elka_data_collection/python/elka_comm__gnd_station.so)
lib1 被称为elka_comm__common.so
,因此它应该在ldd
中显示为库依赖项,对吧?
cmake/make
命令的部分输出:
[100%] Linking CXX shared module ../../../python/elka_comm__gnd_station.so
cd /home/Programs/elka/elka_data_collection/build_elka_data_collection/src/elka_comm/gnd_station && /opt/cmake-3.4.3-Linux-x86_64/bin/cmake -E cmake_link_script CMakeFiles/elka_comm__gnd_station.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -fpic -Wno-as-needed -std=c++11 -fpic -Wno-as-needed -std=c++11 -g -shared -o ../../../python/elka_comm__gnd_station.so CMakeFiles/elka_comm__gnd_station.dir/pyelka_gnd_station.cpp.o CMakeFiles/elka_comm__gnd_station.dir/elka_devices.cpp.o `CMakeFiles/elka_comm__gnd_station.dir/inet_comm.cpp.o -L/home/Programs/elka/elka_data_collection/build_elka_data_collection/src/elka_comm/common -L/home/Programs/elka/elka_data_collection/build_elka_data_collection/src/elka_comm/gnd_station -L/home/Programs/elka/elka_data_collection/build_elka_data_collection/python -L/home/Programs/elka/elka_data_collection/python ../../../python/elka_comm__common.so -Wl,-rpath,/home/Programs/elka/elka_data_collection/build_elka_data_collection/src/elka_comm/common:/home/Programs/elka/elka_data_collection/build_elka_data_collection/src/elka_comm/gnd_station:/home/Programs/elka/elka_data_collection/build_elka_data_collection/python:/home/Programs/elka/elka_data_collection/python`
从这里,在我看来链接是正确的。我最好的直觉是cmake
生成的链接命令中的排序不正确,但除了知道链接命令对排序有特殊性之外,我无法证明这一点。
【问题讨论】:
【参考方案1】:通过将-Wl,--no-as-needed
添加到CMAKE_CXX_FLAGS
来解决。请注意,添加到 CMAKE_SHARED_LINKER_FLAGS|CMAKE_MODULE_LINKER_FLAGS
不起作用,添加 -Wno-as-needed
到 CMAKE_CXX_FLAGS
也不起作用。
不过,其他问题仍然存在。如果有人有经验将 c++ 代码绑定到 python pm 我。
【讨论】:
以上是关于CMAKE将动态库链接到模块,但不显示为链接依赖的主要内容,如果未能解决你的问题,请参考以下文章