[CMake] To avoid target_link_libraries magic

Brad King brad.king at kitware.com
Tue Mar 22 09:03:47 EDT 2011


On 03/22/2011 05:27 AM, Valeriy Bykov wrote:
> I want to build all the project statically using certain version of glibc,
> gcc and all other libs I need. They are situated in the folder "builddeps"
> which I can simply checkout on any host and pass it's path to CMake.

The easiest way to ensure a static link is to use a recent CMake nightly build:

  http://www.cmake.org/files/dev/?C=M;O=D

and set this on your targets:

  set_property(TARGET myexe PROPERTY LINK_SEARCH_START_STATIC 1)

Then no -Bdynamic or -Bstatic options will show if you don't give any shared
libraries to target_link_libraries.  This feature will be included in the next
release of CMake.

> SET (CMAKE_EXE_LINKER_FLAGS ...)
> 
> The second is CMake inserts these system libraries in the front of
> libraries therefore link fails.

The CMAKE_EXE_LINKER_FLAGS is not the place to put libraries for the end
of the link line.  It is meant for the beginning to have things like -m64.

> So it's not obvious in which cases transforming will be applied.

When CMake first runs the ABI check it detects which library search directories
appear in the default link line.  Look at CMakeFiles/CMakeCCompiler.cmake to
see them (CMAKE_C_IMPLICIT_LINK_DIRECTORIES).  Libraries in these directories
will not be passed by full path unless you use an imported target.  When you
put -B/some/path in CMAKE_C_FLAGS then this ends up in the detected implicit
list because the front-end converts it to -L/some/path when invoking the linker.

-Brad


More information about the CMake mailing list