0

如果以前有人问过这个问题,请指出我正确的方向。我有 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生成的链接命令中的排序不正确,但除了知道链接命令对排序有特殊性之外,我无法证明这一点。

4

1 回答 1

2

通过添加-Wl,--no-as-needed解决CMAKE_CXX_FLAGS。请注意,添加 toCMAKE_SHARED_LINKER_FLAGS|CMAKE_MODULE_LINKER_FLAGS不起作用,添加-Wno-as-neededto也不起作用CMAKE_CXX_FLAGS

不过,其他问题仍然存在。如果有人有经验将 c++ 代码绑定到 python pm 我。

于 2017-07-25T22:55:12.960 回答