Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link_directories() bring in extra link flags

Tags:

cmake

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!

like image 504
waitsummer Avatar asked Jan 01 '26 00:01

waitsummer


1 Answers

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

Related (shared libraries)

AFAIK you can ignore this flag if you are not linking to shared libraries:

  • about rpath http://www.cmake.org/Wiki/CMake_RPATH_handling

Related (usage requirements)

Note that this situation is related to library usage requirements (you need to link another library to use library that you install):

  • my answer about install(EXPORT ...)
  • documentation

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!