CMAKE将动态库链接到模块,但不显示为链接依赖项

时间:2017-07-25 18:03:41

标签: c++ cmake linker dynamic-linking pybind11

如果以前曾要求我指出正确的方向。我有lib1和mod2,必须链接在一起。该项目分布在几个文件夹和几个CMakeLists.txt文件中。我正在使用的cmake命令是这样的:

cmake file 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 file 2(lib1 dir):

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 file 3(mod2 dir):

#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 个答案:

答案 0 :(得分:0)

通过将-Wl,--no-as-needed添加到CMAKE_CXX_FLAGS来解决此问题。请注意,添加到CMAKE_SHARED_LINKER_FLAGS|CMAKE_MODULE_LINKER_FLAGS不起作用,也不会将-Wno-as-needed添加到CMAKE_CXX_FLAGS

但其他问题仍然存在。如果有人有经验w /绑定c ++代码到python pm me。