How to specify a compiler in CMake?

后端 未结 5 1497
粉色の甜心
粉色の甜心 2020-12-03 02:50

I would like to use the IAR compiler. I noticed CMake has already have a bunch of files about this compiler:

https://github.com/jevinskie/cmake/blob/master/Modules/C

5条回答
  •  佛祖请我去吃肉
    2020-12-03 03:39

    I see more and more people who set CMAKE_C_COMPILER and other compiler-related variables in the CMakeLists.txt after the project call and wonder why this approach breaks sometimes.

    What happens actually

    When CMake executes the project() call, it looks for a default compiler executable and determines the way for use it: default compiler flags, default linker flags, compile features, etc.

    And CMake stores path to that default compiler executable in the CMAKE_C_COMPILER variable.

    When one sets CMAKE_C_COMPILER variable after the project() call, this only changes the compiler executable: default flags, features all remains set for the default compiler.

    AS RESULT: When the project is built, a build system calls the project-specified compiler executable but with parameters suitable for the default compiler.

    As one could guess, this approach would work only when one replaces a default compiler with a highly compatible one. E.g. replacement of gcc with clang could work sometimes.

    This approach will never work for replacement of cl compiler (used in Visual Studio) with gcc one. Nor this will work when replacing a native compiler with a cross-compiler.

    What to do

    Never set a compiler in CMakeLists.txt.

    If you want, e.g., to use clang instead of defaulted gcc, then either:

    1. Pass -DCMAKE_C_COMPILER= to cmake when configure the project. That way CMake will use this compiler instead of default one and on the project() call it will adjust all flags for the specified compiler.

    2. Set CC environment variable (CXX for C++ compiler). CMake checks this variable when selects a default compiler.

    3. (Only in rare cases) Set CMAKE_C_COMPILER variable before the project() call. This approach is similar to the first one, but makes the project less flexible.

    If the ways above do not work

    If on setting CMAKE_C_COMPILER in the command line CMake errors that a compiler cannot "compile a simple project", then something wrong in your environment.. or you specify a compiler incompatible for chosen generator or platform.

    Examples:

    • Visual Studio generators work with cl compiler but cannot work with gcc.
    • A MinGW compiler usually requires MinGW Makefiles generator.

    Incompatible generator cannot be fixed in CMakeLists.txt. One need to pass the proper -G option to the cmake executable (or select the proper generator in CMake GUI).

    Cross-compiling

    Cross-compiling usually requires setting CMAKE_SYSTEM_NAME variable, and this setting should normally be done in the toolchain file. That toolchain file is also responsible for set a compiler.

    Setting CMAKE_SYSTEM_NAME in the CMakeLists.txt is almost always an error.

提交回复
热议问题