[cmake-developers] GCC link options

Brad King brad.king at kitware.com
Wed Jul 25 14:31:48 EDT 2012


On 07/25/2012 02:12 PM, J Decker wrote:
> target_link_libraries(  playcard --whole-archive
> eltanin_sdl_deadstart_end --no-whole-archive ${COMMON_LIBRARIES} )
> (the output link line looks like .....  libplaycard.a --whole-archive
> eltanin_sdl_deadstart_end.a libcommon.a --no-whole-archive )

The only ordering guarantee is that the target being linked
(final executable or shared library) has its own sequence
first as specified by target_link_libraries.  Transitive
dependencies of the libraries named at the first level will
all appear in order but may have other things inserted.

The only reliable place to put flags is in the top-level call
to target_link_libraries for the target getting linked.  For
example:

 add_executable(foo foo.c)
 target_link_libraries(foo ${libs}
   --whole-archive ${special} --no-whole-archive)

will result in

 -o foo ${libs} --whole-archive ${special} --no-whole-archive ${depends}


Alternatively, using CMake 2.8.8 you can also use an object library:

 add_library(eltanin_sdl_deadstart_end OBJECT src.c)
 add_executable(foo foo.c $<TARGET_OBJECTS:eltanin_sdl_deadstart_end>)
 target_link_libraries(foo ${libs})

That will ensure the desired objects end up in the executable.
They will be listed explicitly on the link line as ".o" files.

-Brad



More information about the cmake-developers mailing list