2017-07-25 117 views
0

如果之前詢問過,請指向正確的方向。我有lib1和mod2,它們必須鏈接在一起。這個項目分散到幾個文件夾和幾個CMakeLists.txt文件。我使用的cmake命令是這樣:CMAKE將動態庫鏈接到模塊,但不顯示爲鏈接依賴關係

cmake的文件1(基DIR):

# 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 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的文件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 
) 

我的幾個步驟是多餘的,因爲完整性檢查(例如,設置標誌W/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生成的鏈接命令的排序是不正確的,但我不能證明這一點,除了知道鏈接命令對於排序是特定的。

回答

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下午我。