Reputation: 43
I am exhausted by this problem... I have spend several days on it.
I use target_link_libraries link A with B and C
target_link_libraries(A rootdir/B.lib rootdir/C.lib)
while B need some other files in E and F directory, I use
link_directories(rootdir/E rootdir/F)
to include the directory E and F, but using make VERBOSE=1 I found although cmake add -i before the E and F and passed them to the link, it also add some extra flags such as
-Wl,-rpath, rootdir/E:rootdir/F:
where does these extra parameters come from? How can I fix this problem? I will be grateful for any help! Thanks!
Upvotes: 3
Views: 2718
Reputation:
The "problem" is in command link_directories
:
> cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Boo)
link_directories("/path/to/Foo")
add_executable(boo Boo.cpp)
target_link_libraries(boo "/path/to/Foo/libFoo.a")
> cmake -H. -B_builds -DCMAKE_VERBOSE_MAKEFILE=ON
> cmake --build _builds
/usr/bin/c++ ... -o boo -Wl,-rpath,/.../Foo
If you use find_library
command to find Foo
and link it using target_link_libraries
flags
will be removed:
> cat CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Boo)
add_executable(boo Boo.cpp)
find_library(LIBFOO Foo HINTS "/path/to/Foo/")
if(NOT LIBFOO)
message(FATAL_ERROR "Library `Foo` not found in `/path/to/Foo`")
endif()
target_link_libraries(boo ${LIBFOO})
> cmake -H. -B_builds -DCMAKE_VERBOSE_MAKEFILE=ON
> cmake --build _builds
/usr/bin/c++ CMakeFiles/boo.dir/Boo.cpp.o -o boo -rdynamic /path/to/Foo/libFoo.a
AFAIK you can ignore this flag if you are not linking to shared libraries:
Note that this situation is related to library usage requirements (you need to link another library to use library that you install):
install(EXPORT ...)
Upvotes: 2