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

时间:2021-07-12 04:55:05

Point me in the right direction if this has been asked before. I have lib1 and mod2, which must be linked together. This project is spread to a couple of folders and a couple of CMakeLists.txt files. The cmake commands that I am using are as such:

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

cmake file 1 (base dir):

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

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

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
)

A few of my steps are redundant as sanity checks (e.g. setting flags w/CMAKE variables).

我的一些步骤是冗余的,因为健全性检查(例如设置带有CMAKE变量的标志)。

The following is the partial output of ldd -r <path-to-mod2.so>/mod2.so:

以下是ldd -r /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 is called elka_comm__common.so, and so it should show up as a library dependency in ldd, right?

lib1被称为elka_comm__common.so,所以它应该在ldd中显示为库依赖,对吗?

Partial output of cmake/make commands:

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`

From this, it seems to me that linking is done correctly. My best intuition is that the ordering in the cmake generated link command is incorrect, but I can't justify this other than knowing that link commands are particular about ordering.

从这一点来看,在我看来,链接是正确完成的。我最好的直觉是cmake生成的链接命令中的顺序是不正确的,但除了知道链接命令特别关于排序之外,我无法证明这一点。

1 个解决方案

#1


0  

Solved by adding -Wl,--no-as-needed to CMAKE_CXX_FLAGS. Be mindful that adding to CMAKE_SHARED_LINKER_FLAGS|CMAKE_MODULE_LINKER_FLAGS doesn't work, and neither does adding -Wno-as-needed to CMAKE_CXX_FLAGS.

解决方法是添加-Wl, - 不需要CMAKE_CXX_FLAGS。请注意,添加到CMAKE_SHARED_LINKER_FLAGS | CMAKE_MODULE_LINKER_FLAGS不起作用,也不会向CMAKE_CXX_FLAGS添加-Wno-as-needed。

Other issues persist, though. If anyone is experienced w/binding c++ code to python pm me.

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

#1


0  

Solved by adding -Wl,--no-as-needed to CMAKE_CXX_FLAGS. Be mindful that adding to CMAKE_SHARED_LINKER_FLAGS|CMAKE_MODULE_LINKER_FLAGS doesn't work, and neither does adding -Wno-as-needed to CMAKE_CXX_FLAGS.

解决方法是添加-Wl, - 不需要CMAKE_CXX_FLAGS。请注意,添加到CMAKE_SHARED_LINKER_FLAGS | CMAKE_MODULE_LINKER_FLAGS不起作用,也不会向CMAKE_CXX_FLAGS添加-Wno-as-needed。

Other issues persist, though. If anyone is experienced w/binding c++ code to python pm me.

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