项目A依赖共享库B,B依赖C,A不依赖C。当我使用B的API时,警告B找不到C

时间:2019-09-09 05:48:35

标签: c++ cmake

项目A依赖于共享库BB依赖于CA不依赖于CB被编译并安装为共享库。当A使用B的API时,会出现警告,提示B找不到C,如代码所示。

在该代码中,ALibTestBTdLibCpangolin

Scanning dependencies of target LibTest
[ 50%] Building CXX object CMakeFiles/LibTest.dir/main.cpp.o
[100%] Linking CXX executable LibTest
/usr/bin/ld: warning: libpangolin.so, needed by /home/wang/CLionProjects/TdLibrary/install/lib/libTdLib.so, not found (try using -rpath or -rpath-link)
/home/wang/CLionProjects/TdLibrary/install/lib/libTdLib.so: 'pangolin::CreateDisplay()’ undefined reference
...
collect2: error: ld returned 1 exit status
CMakeFiles/LibTest.dir/build.make:84: recipe for target 'LibTest' failed
make[3]: *** [LibTest] Error 1
CMakeFiles/Makefile2:109: recipe for target 'CMakeFiles/LibTest.dir/all' failed
make[2]: *** [CMakeFiles/LibTest.dir/all] Error 2
CMakeFiles/Makefile2:121: recipe for target 'CMakeFiles/LibTest.dir/rule' failed
make[1]: *** [CMakeFiles/LibTest.dir/rule] Error 2
Makefile:131: recipe for target 'LibTest' failed
make: *** [LibTest] Error 2

共享库CMakelist.txt的{​​{1}}。

B

我通过{p>安装add_library(TdLib SHARED pangolin_tools.cpp pangolin_tools.h ) #pangolin find_package(Pangolin REQUIRED) include_directories(${Pangolin_INCLUDE_DIRS} ) set(HEADER_FILES pangolin_tools.h ) set_target_properties(TdLib PROPERTIES PUBLIC_HEADER "${HEADER_FILES}" ) target_link_libraries(TdLib PRIVATE ${Pangolin_LIBRARIES} ) install(TARGETS TdLib # IMPORTANT: Add the foo library to the "export-set" EXPORT ${PROJECT_NAME}Targets RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT shlib PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/TdLibrary" COMPONENT dev)

B

项目cmake .. -DCMAKE_INSTALL_PREFIX:=/home/wang/CLionProjects/TdLibrary/install make make install 的{​​{1}}。

CMakelist.txt

1 个答案:

答案 0 :(得分:0)

您肯定会链接到共享库。

在下面,我将删除PRIVATE关键字:

target_link_libraries(TdLib PRIVATE
                    ${Pangolin_LIBRARIES}
                    )

,看看是否有帮助。最后,我注意到您可以不带任何target_link_libraries()来链接库,这会迫使您在构建可执行文件时添加这些库。

我还猜想这个PRIVATE关键字可能与您的想法完全不符。