[CMake] How to use 'CMAKE_EXE_LINKER_FLAGS' correctly

Chuck Atkins chuck.atkins at kitware.com
Fri May 20 10:55:33 EDT 2016


> -rpath


 RPATHs are automatically added by CMake to executables so they can use
libraries from the build tree.



> -whole-archive
>

whole-archive is definitely trickier since you shouldn't be applying it to
the entire executable but instead wrapping individual libraries with it.
Conveniently, you can pass link options directly with
target_link_libraries.  So you could have:

# Just an example, find_library calls should really be isolated to separate
find modules
find_library(FOO_LIBRARY foo)
set(FOO_LIBRARY "-Wl,--whole-archive ${FOO_LIBRARY} -Wl,--no-whole-archive")

add_executable(hello main.c)
target_link_libraries(hello ${FOO_LIBRARY})


>> set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L . -lhello")
>

CMAKE_EXE_LINKER_FLAGS should work for other more general cases that aren't
tied to specific libraries.  The problem with your example is likely not
using an absolute path for -L since the compilation is actually taking
place in a nested work directory somewhere.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160520/cd781bcf/attachment.html>


More information about the CMake mailing list