[CMake] Set C compiler flags but not linker flags

Roland Schulz roland at utk.edu
Sat Jul 28 17:10:08 EDT 2012


Hi,

I found a solution and thus answer to myself in case other (will) have
a similar issue. For shared libraries
CMAKE_C_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS exists but no equivalent
option for executables is available.
Instead one is required to use set_target_properties(COMPILE_FLAGS
"...") to avoid that the flags are added to the linker. See also:
http://www.cmake.org/pipermail/cmake/2011-March/043365.html

In that case OpenMP requires the linker flags to be set using
target_link_libraries. This works correctly on Unix because all flags
are starting with "-" and anything starting with "-" is understood by
cmake as flag not library file. On Windows (MSVC/ICC) this wouldn't
work because the default openmp linker flag is "/openmp" and this is
misunderstood by cmake as library file and turned into "\openmp".
Luckily the compilers doesn't require the /openmp flag for linking so
this isn't a problem in this case.

For the future it would be nice if cmake would have
CMAKE_C_CREATE_EXE_LINKER_FORBIDDEN_FLAGS
and/or
would understand that "/something" in target_link_libraries is a flag
not a file.

Roland

On Fri, Jul 13, 2012 at 1:10 AM, Schulz, Roland <rschulz3 at utk.edu> wrote:
> Hi,
>
> if I set compiler flags using CMAKE_C_FLAGS these flags are also added
> to the linker flags for C executables and libraries. How can I set
> compiler flags without those same flags also appended to the linker
> flags?
>
> In this specific case I would like to add "-fopenmp" to the compiler
> but do not want this flag to be added to the linker because I want the
> openmp library to be linked statically.  For GCC this can be done with
> "-Wl,-static -lgomp -lrt -Wl,-Bdynamic -lpthread" but only works if
> "-fopenmp" is not also given as a linker flag. Thus it is important
> that "-fopenmp" which is needed as compile time option is not appended
> to the linker flags.
>
> This is with GCC on Linux with "Unix Makesfiles" and cmake 2.8.8.
>
> Roland
> --
> ORNL/UT Center for Molecular Biophysics cmb.ornl.gov
> 865-241-1537, ORNL PO BOX 2008 MS6309
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
>
>
>



-- 
ORNL/UT Center for Molecular Biophysics cmb.ornl.gov
865-241-1537, ORNL PO BOX 2008 MS6309


More information about the CMake mailing list