[CMake] Idomatic cmake: Supporting gcc, icc, and clang with custom flags for each?

Chuck Atkins chuck.atkins at kitware.com
Tue May 17 09:47:13 EDT 2016


Hi TCW,

A typical approach for this is in the top level CMakeLists.txt to have:

include(CompilerFlags)

And then you can isolate the detection and specialization logic in a
separate CompilerFlags.cmake:

if(CMAKE_COMPILER_IS_GNUC)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS" -extra --gcc-options")
elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS" -extra --icc-options")
elseif(CMAKE_C_COMPILER_ID MATCHES "PGI)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS" -extra --pgcc-options")
elseif(MSVC)
  if(MSVC_VERSION GREATER_THAN 1700)
    ...
  elseif(...)
    ...
  endif()
endif()

And then similarly for C++.


- Chuck

On Tue, May 17, 2016 at 1:49 AM, TCW via CMake <cmake at cmake.org> wrote:

> Hello all,
>
> On linux, what's the correct way to support building with several
> different C compilers, each with some extra compiler-specifc flags for
> Debug and Release modes?  (Eventually I'll need to add Visual Studio on
> Windows too. )
>
> For now, I'm following what's mentioned in the cmake FAQ and using
> CXX=/blah cmake, etc.
>
> (From:
> https://cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F)
>
> But this is getting quite cumbersome as I'm experimenting with different
> flags for each compiler, and I'd rather avoid polluting my fairly clean
> CMakeLists file with a bunch of if/else branches to set flags.
>
> In the cmake manual I see reference to a -DCMAKE_TOOLCHAIN_FILE option,
> but this seems designed for embedded cross-compile scenarios.  (Is that
> right?)
>
> (From: https://cmake.org/cmake/help/v3.0/manual/cmake-toolchains.7.html)
>
> Basically, I'd like to succinctly choose among a set of (slightly)
> customized compiler choices.
>
> For modern cmake usage what is the correct method?  Can anybody point me
> to a well done example for a simple case like this?
>
> Thank you!
> tcw
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160517/0a3c1e16/attachment.html>


More information about the CMake mailing list