sazpw
sazpw

Reputation: 23

cmake target_link_libraries() cannot find renamed lib target by set_target_properties(archive_output_name)

RT~ ps: cmake version 3.9.2

My codebase just like this.

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

my thirdparty/hellolib/CMakeLists.txt is

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")

my test/CMakeLists.txt is

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)

when I build my top-level project, an error occurs like this.

/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

But when I comment the line # SET_TARGET_PROPERTIES(hello_static PROPERTIES ARCHIVE_OUTPUT_NAME "hello") and TARGET_LINK_LIBRARIES with hello_static, everything goes fine.

It seems that TARGET_LINK_LIBRARIES cannot find renamed lib target. Could anyone explain it? thanks in advance.

Upvotes: 1

Views: 823

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65936

It seems that TARGET_LINK_LIBRARIES cannot find renamed lib target.

Setting ARCHIVE_OUTPUT_NAME property renames not a target, but an output file. So linking with a target still works:

TARGET_LINK_LIBRARIES(main hello_static)

One cannot rename the target once it is created, but it is possible to create ALIAS for a target:

ADD_LIBRARY(hello ALIAS hello_static)

After that it is possible to link with the alias:

TARGET_LINK_LIBRARIES(main hello)

Upvotes: 2

Related Questions