6.指定编译器

指定编译器


目前为止,我常用的编译器都集中在交叉编译,例如arm,arm64等。

CMake可以根据平台和生成器选择编译器,还能将编译器标志设置为默认值。

然而,我们通常控制编译器的选择。

在后面的示例中,我们还将考虑构建类型的选择,并展示如何控制编译器标志。

具体实施


CMake将语言的编译器存储在 CMAKE__COMPILER 变量中,其中是受支持的任何一种语言,

对于我们的目的是 CXX 、 C 或 Fortran 。用户可以通过以下两种方式之一设置此变量:

  1. ``` cmake

    使用CLI中的 -D 选项,例如:

    cmake -D CMAKE_CXX_COMPILER=clang++ ..
    1
    2
    3
    4
    5

    2. ``` cmake
    # 通过导出环境变量 CXX (C++编译器)、 CC (C编译器)和 FC (Fortran编译器)。
    # 例如,使用这个命令使用 clang++ 作为 C++ 编译器:
    env CXX=clang++ cmake ..

编译器选项

  • CMAKE_CXX_COMPILER
  • CMAKE_C_COMPILER
  • CMAKE_Fortran_COMPILER
实例运行输出结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ cmake ..

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.29.30038.1
-- The CXX compiler identification is MSVC 19.29.30038.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- ##### Is the C++ compiler loaded? 1 #####
-- ##### The C++ compiler ID is: MSVC #####
-- ##### Is the C++ from GNU? #####
-- ##### The C++ compiler version is: 19.29.30038.1 #####
-- ##### Is the C compiler loaded? 1 #####
-- ##### The C compiler ID is: MSVC #####
-- ##### Is the C++ from GNU? #####
-- ##### The C compiler version is: 19.29.30038.1 #####
-- Configuring done

CMakeLists.txt文件

在CMakeLists.txt文件中通常如下指定,不用是注释掉,用的时候取消注释。

1
2
3
4
5
6
7
8
9
#[[#FT
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER "/bin/aarch64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "/bin/aarch64-linux-gnu-g++")]]

#[[#7045
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER "/bin/arm-linux-gnueabihf-gcc")
set(CMAKE_CXX_COMPILER "/bin/arm-linux-gnueabihf-g++")]]

例子1:CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 注意,这个例子不包含任何目标,没有要构建的东西,我们只关注配置步骤:

cmake_minimum_required(VERSION 3.5)

project(hello-world LANGUAGES C CXX)

message(STATUS "##### Is the C++ compiler loaded? ${CMAKE_CXX_COMPILER_LOADED}")
if (${CMAKE_CXX_COMPILER_LOADED})
message(STATUS "##### The C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "##### Is the C++ from GNU? ${CMAKE_COMPILER_IS_GNUCXX}")
message(STATUS "##### The C++ compiler version is: ${CMAKE_CXX_COMPILER_VERSION}")
endif()

message(STATUS "##### Is the C compiler loaded? ${CMAKE_C_COMPILER_LOADED}")
if (${CMAKE_C_COMPILER_LOADED})
message(STATUS "##### The C compiler ID is: ${CMAKE_C_COMPILER_ID}")
message(STATUS "##### Is the C++ from GNU? ${CMAKE_COMPILER_IS_GNUCC}")
message(STATUS "##### The C compiler version is: ${CMAKE_C_COMPILER_VERSION}")
endif()

例子2:CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 3.5)

project(hello-world LANGUAGES C CXX)

option(CMAKE_CXX_COMPILER clang++)
message(STATUS "##### 编译器:${CMAKE_CXX_COMPILER_ID}")
message(STATUS "##### 编译器版本:${CMAKE_CXX_COMPILER_VERSION}")

option(USE_LIBRARY OFF)
message(STATUS "##### USE_LIBRARY? ${USE_LIBRARY}")

list(APPEND _sources Message.h Message.cpp)

if (${USE_LIBRARY})
add_library(message ${_sources})
add_executable(hello-world hello-world.cpp)
target_link_libraries(hello-world message)
else()
add_executable(hello-world hello-world.cpp message)
endif()