When setting link libraries in the following manner
target_link_libraries (SOME_TARGET -L/somedir -lfoo)
cmake doesn't handle RPATHs. Is using '-L' and '-l' not best practice, or actually plain wrong? When creating my own Find*.cmake I usually use find_library()
but the find script I got doesn't do this and resorts to the above form using '-L' and '-l'.
The documentation doesn't really explain how RPATHs are gathered, also the documentation isn't really clear how it handles "-l" and "-L" the only pointer you get is
"Item names starting with -, but not -l or -framework, are treated as linker flags"
Basically, You're using
target_link_libraries()
wrong. According to documentation, You should provide target, libraries and maybe some CMake specific linkage flags.For example something like that:
If You're using Your own crafted
Find*.cmake
solutions, it's usualy being done like this:NOTE: I assume Your crafted
Find*.cmake
files follows these guidelines and fills CMake variables likeSOMELIB_LIBRARIES
, and/orSOMELIB_INCLUDE_DIRS
, etc.NOTE2: for my personal opinion,
target_link_directories()
is pain in a butt and You should avoid using it if not really needed. It's difficult to maintain and uses paths relative to current source directory.Specifying toolchain-dependent flags like
-l
and-L
is generally not recommended, as it breaks portability and might have different effects than you expect.The correct way to set the linker path would be the
link_directories
command.The idiomatic solution in CMake is to use
find_library
for locating the library and then pass the full path to the linker, so you do not need to worry about link directories at all.Now, the RPATH is a different beast, as it also determines where dynamic libraries can be located at runtime. Usually, the default settings work reasonably fine here. If you ever find yourself in the unfortunate situation where it does not, there is a number of target properties and CMake variables influencing this:
(From the
set_target_properties
docs)Also, you might want to have a look at the CMake Wiki page for RPATH handling.
The whole RPATH business is unfortunately rather complex and a thorough explanation would require far more space than is appropriate for a StackOverflow answer, but I hope this is enough to get you started.