CMake省略传递给'target_link_libraries'的库

时间:2017-05-04 18:38:48

标签: makefile cmake linker linker-errors

我正在尝试使用我编写的CMake文件构建一组自定义库。当前目录结构如下所示:

./lib

我的目标是在链接./src目录中的可执行文件时让编译器使用/* minimum version, project name etc */ set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/lib) set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/lib) /* other stuff */ set(CDH_LIB_NAMES comm daemon debug tty sock) set(CDH_LIB_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/lib/global.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/libcomm.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/libdaemon.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/libdebug.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/libtty.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/libsock.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/uthash.h ${CMAKE_CURRENT_SOURCE_DIR}/lib/utlist.h) add_subdirectory(lib) # make sure we build .so first include_directories(lib) # then use this as an include directory link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) # and a link directory find_library(CDH_LIBS NAMES tty comm sock debug daemon PATHS "${CMAKE_CURRENT_SOURCE_DIR}/lib" ) # Not sure if this does anything add_subdirectory(src) # Now build the src binaries 目录中内置的.so文件。我试过以下,但无济于事:

在顶级CMakeLists.txt:

target_link_libraries()

我还听说你可以使用set(ACD_BIN ACdtest) set(ACD_SRC ${CMAKE_CURRENT_SOURCE_DIR}/ACd.c ${CMAKE_CURRENT_SOURCE_DIR}/ACd.h ${CDH_LIB_INCLUDES}) add_executable(${ACD_BIN} ${ACD_SRC}) target_link_libraries(${ACD_BIN} ${CDH_LIB_NAMES}) ,所以我在所有`src / * / CMakeLists.txt中添加了与此类似的行:

make VERBOSE=1 pi-d

出于某种原因,我仍然得到未定义的引用。所以我尝试用Linking C executable pi-d cd /home/user/projectname/build/src/pi_d && /usr/bin/cmake -E cmake_link_script CMakeFiles/pi-d.dir/link.txt --verbose=1 /usr/bin/cc --std=gnu99 CMakeFiles/pi-d.dir/PId.c.o -o pi-d -L/home/user/projectname/lib -rdynamic ../../lib/libcomm.so ../../lib/libdaemon.so ../../lib/libsock.so -Wl,-rpath,/home/user/projectname/build/lib:/home/user/projectname/lib 编译一个这样的二进制文件并得到以下输出(为了简洁起见只有相关的行):

libcomm

看起来它只是libdaemonlibsocklibdebug,而忽略了libtty<div aria-live="polite"> <button>Toggle details</button> </div> 。为什么会这样?我能改变什么?

1 个答案:

答案 0 :(得分:2)

debugtarget_link_libraries命令的关键字

与库链接后,该关键字仅针对“调试”配置执行,这可能不是您的情况。