3

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!

waitsummer
  • 43
  • 2
  • 8

1 Answers1

2

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:

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

Community
  • 1
  • 1
  • Thanks a lot for your great help. I use link_directories because the linked library b.lib need some other files(just common files, not library), these files are pretty much and all in different directories, if I use find_library I need to write a lot of find_library commands. I consider it is a common requirement that the linked lib maybe need to include some other files and directories, why there is not a way to meet this requirement? sure, there is a way to add the included directories by link_directories, but it is not a workable way. – waitsummer Jan 06 '14 at 02:43
  • I can't understand why it pass some flags which my linker can not recognize, even with no the extra flags the colon added between the included directories also can not be recognized by my linker, the linker will report a error can not find "rootdir/E:rootdir/F". I really can't understand this....... I think link_directories add -i before the directory and pass these parameters to linker is enough... – waitsummer Jan 06 '14 at 02:47
  • @waitsummer `why there is not a way to meet this requirement` see my notes about usage requirements, read [documentation](http://www.cmake.org/cmake/help/git-next/manual/cmake-packages.7.html#config-file-packages) about `find_package` and my answers: [1](http://stackoverflow.com/questions/20833226/library-headers-and-define/20838147#20838147), [2](http://stackoverflow.com/questions/20746936/cmake-of-what-use-is-find-package-if-you-need-to-specify-cmake-module-path-an/20857070#20857070) IMHO it is exactly what your need –  Jan 06 '14 at 08:43
  • @waitsummer `I can't understand why it pass some flags which my linker can not recognize` what OS, compiler, cmake version are you using? I try my linux ubuntu with gcc and it working fine, path to linker is separated by `:` without error –  Jan 06 '14 at 08:46