RT〜ps:cmake version 3.9.2
我的代码库就是这样。
suzanwen@n224-004-133:~/repos/C++/ttt:)$ tree -L 2
.
├── build
│ ├── bin
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── lib
│ ├── Makefile
│ ├── test
│ └── thirdparty
├── build.sh
├── CMakeLists.txt
├── Makefile
├── test
│ ├── CMakeLists.txt
│ └── main.cc
└── thirdparty
├── CMakeLists.txt
├── gflags
└── hellolib
10 directories, 9 files
我的第三方/hellolib/CMakeLists.txt是
PROJECT(hello)
SET(LIBHELLO_SRC hello.cc)
MESSAGE(STATUS "LIBRARY PATH=" ${LIBRARY_OUTPUT_PATH})
ADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC})
SET_TARGET_PROPERTIES(hello_static PROPERTIES ARCHIVE_OUTPUT_NAME "hello")
我的测试/CMakeLists.txt是
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/thirdparty/hellolib
${PROJECT_SOURCE_DIR}/thirdparty/gflags/include)
IF(LIBRARY_OUTPUT_PATH)
LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH})
ENDIF(LIBRARY_OUTPUT_PATH)
ADD_EXECUTABLE(main main.cc)
TARGET_LINK_LIBRARIES(main hello)
# TARGET_LINK_LIBRARIES(main hello_static)
当我构建顶级项目时,会出现这样的错误。
/usr/bin/c++ -rdynamic CMakeFiles/main.dir/main.cc.o -o ../bin/main -L/home/suzanwen/repos/C++/ttt/build/lib -Wl,-rpath,/home/suzanwen/repos/C++/ttt/build/lib -lhello
/usr/bin/ld: cannot find -lhello
但是当我用# SET_TARGET_PROPERTIES(hello_static PROPERTIES ARCHIVE_OUTPUT_NAME "hello")
注释行TARGET_LINK_LIBRARIES
和hello_static
时,一切正常。
似乎TARGET_LINK_LIBRARIES
找不到重命名的lib目标。有人可以解释吗?预先感谢。
答案 0 :(得分:2)
似乎
TARGET_LINK_LIBRARIES
找不到重命名的lib目标。
设置ARCHIVE_OUTPUT_NAME
属性不会重命名目标,而是重命名输出文件。因此,与 target 链接仍然有效:
TARGET_LINK_LIBRARIES(main hello_static)
创建目标后,便无法重命名目标,但是可以为目标创建ALIAS:
ADD_LIBRARY(hello ALIAS hello_static)
之后,可以链接别名:
TARGET_LINK_LIBRARIES(main hello)