[CMake] How to append arbitrary linker options?

Brad King brad.king at kitware.com
Thu Jun 4 17:20:09 EDT 2009


Bartlett, Roscoe A wrote:
> I would like to be able to append arbitrary linker options to the end of 
> my link lines on Unix/Linux systems.  However, the options set in the 
> CMAKE_EXE_LINKER_FLAGS variable are listed *before* all of the libraries 
> that CMake knows about.  I need to be able to append a bunch of nasty 
> options like Fortran libraries, MPI libraries (in some nasty cases) and 
> other libraries that must come after all other libraries.

While working on something else I noticed something in the CMake source
that will allow one to do this.  There is an undocumented variable meant
for use by Modules/Platform/*.cmake files to specify compiler runtime
libraries: CMAKE_<LANG>_STANDARD_LIBRARIES.

I won't claim that this is officially supported, but it does work in
CMake 2.6:

   set(CMAKE_C_STANDARD_LIBRARIES ${NASTY_FLAGS})
   set(CMAKE_CXX_STANDARD_LIBRARIES ${NASTY_FLAGS})
   set(CMAKE_Fortran_STANDARD_LIBRARIES ${NASTY_FLAGS})

The <LANG> that is chosen is the linker language for the target.  Note
that some platform files (for MSVC, at least) do set these variables so
it is important to prepend to them:

   set(CMAKE_CXX_STANDARD_LIBRARIES ${NASTY_FLAGS}
       ${CMAKE_CXX_STANDARD_LIBRARIES})

(it also gets cached if a platform file defines it)

The value of this variable is always added to the end of the link
line for shared libraries and executables.  It is independent of all
link dependency analysis.

-Brad


More information about the CMake mailing list