使用不同编译器

强制使用命令行选择编译器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 强制使用 Clang
cmake -S . -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang

# 强制使用 GCC (确保已经在环境变量中)
cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++

# 强制使用 msvc
# 方式 A:使用默认的 Visual Studio 生成器(生成 .sln 工程)
cmake -S . -B build_msvc -G "Visual Studio 17 2022" -A x64
# 方式 B:使用 Ninja 生成器(更推荐,速度更快,但需要进入“开发者命令提示符”)
cmake -G "Ninja" -S . -B build_msvc -DCMAKE_C_COMPILER=cl.exe -DCMAKE_CXX_COMPILER=cl.exe

# 强制使用 intel compile
# 使用 Intel LLVM 编译器驱动
# 注意:通常需要先运行 oneAPI 的 setvars.bat 环境脚本
cmake -G "Ninja" -S . -B build_intel -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icx

⚠️ 命令行操作的 3 个“深坑”提醒

  1. 路径中的空格:如果编译器路径包含空格(如 C:\Program Files\...),必须用双引号包裹路径,例如 -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe"
  2. 混合使用生成器千万不要在一个已经生成的 build 文件夹里切换编译器。
    • 后果:CMake 会报错“编译器不匹配”。
    • 对策:切换编译器前,必须手动删除整个 build 文件夹,或者为每个编译器指定不同的目录(如 build_gcc, build_msvc)。
  3. MSVC 的环境限制:在纯命令行中使用 cl.exe (MSVC) 或 icx (Intel) 时,普通的 CMD 往往找不到编译器。
    • 对策:在 Windows 搜索栏输入 **”Developer Command Prompt for VS 2022”**,在那个特制的命令行窗口里执行命令。

使用CMakePresets.json

1.gcc版本CMakePresets.json

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
"version": 3,
"configurePresets": [
{
"name": "base-gcc",
"displayName": "GCC Base Configuration",
"description": "通用 GCC 配置基础层",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "gcc.exe",
"CMAKE_CXX_COMPILER": "g++.exe"
}
},
{
"name": "gcc-debug",
"displayName": "GCC Debug",
"inherits": "base-gcc",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "gcc-release",
"displayName": "GCC Release",
"inherits": "base-gcc",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "gcc-debug",
"configurePreset": "gcc-debug"
},
{
"name": "gcc-release",
"configurePreset": "gcc-release"
}
]
}

2. 如何在命令行中使用

有了这个文件,再也不需要敲长命令了,只需要执行:

配置阶段 (Configure)

1
2
3
4
5
# 配置为 Debug 版本
cmake --preset=gcc-debug

# 或者配置为 Release 版本
cmake --preset=gcc-release

构建阶段 (Build)

1
2
# 直接构建
cmake --build --preset=gcc-debug

3. 关键点说明

  • Generator (生成器): 这里使用了 "Ninja"。在 Windows 上配合 GCC 时,Ninja 的速度远超 MinGW Makefiles。请确保安装了 Ninja(VS2022 默认带了,路径通常在 Path 里)。

  • Inheritance (继承): 使用了 "inherits" 关键字。这样只需要在一个地方 (base-gcc) 定义编译器路径,Debug 和 Release 模式会自动继承它,减少重复代码。

  • Path (路径): 这里的 "gcc.exe" 是简写。如果 GCC 没有添加到系统环境变量 Path 中,需要把这里改写成绝对路径(注意用正斜杠),例如:"C:/msys64/mingw64/bin/g++.exe"

  • 命令行与 CMakePresets.json 的交互逻辑: 当执行 cmake --preset 时,CMake 会在当前目录下自动寻找 CMakePresets.json。它会读取文件里的 generatorcacheVariables(编译器路径)和 environment,然后自动应用到命令中。


4. 常见问题排查

如果在执行 cmake --preset gcc-debug 时报错:

  1. 找不到编译器:检查 gcc --version 是否能在终端运行。如果不能,请手动补全 JSON 里的绝对路径。
  2. 找不到 Ninja:如果报错找不到 Generator,可以将 "generator": "Ninja" 改为 "generator": "MinGW Makefiles",但此时需要确保 mingw32-make 在你的路径中。

5. 命令行微调

如果偶尔需要用一个极其特殊的编译器版本,可以在命令行覆盖 Preset:

1
cmake --preset=gcc-debug -DCMAKE_CXX_COMPILER=C:/special/g++.exe

6. 验证是否生效

如果想看 CMake 到底识别了哪些预设,可以在项目目录下输入:

1
2
cmake --list-presets
cmake --build --list-presets

其他编译器的CMakePresets.json写法

clang编译器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"version": 3,
"configurePresets": [
{
"name": "clang-win-x64",
"displayName": "Clang x64 (Windows)",
"description": "使用 LLVM Clang 编译器",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang-cl.exe",
"CMAKE_CXX_COMPILER": "clang-cl.exe",
"CMAKE_BUILD_TYPE": "Debug"
}
}
],
"buildPresets": [
{
"name": "clang-build",
"displayName": "Clang Build",
"configurePreset": "clang-win-x64"
}
]
}

构建命令

1
2
cmake --preset=clang-win-x64
cmake --build --preset=clang-build

intel编译器

  1. 需要先运行脚本

    1
    "C:\Program Files (x86)\Intel\oneAPI\setvars.bat" intel64
  2. 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"version": 3,
"configurePresets": [
{
"name": "intel-icx-x64",
"displayName": "Intel C++ Compiler",
"description": "使用 Intel oneAPI icx 编译器",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "icx.exe",
"CMAKE_CXX_COMPILER": "icx.exe",
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "intel-build",
"configurePreset": "intel-icx-x64"
}
]
}

构建命令

1
2
3
cmake --preset=intel-icx-x64
cmake --build --preset=intel-build

msvc编译器

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
26
27
28
29
30
31
{
"version": 3,
"configurePresets": [
{
"name": "msvc-x64-debug",
"displayName": "MSVC x64 Debug",
"description": "使用 Visual Studio 2022 编译器 (cl.exe)",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe",
"CMAKE_BUILD_TYPE": "Debug"
},
"architecture": {
"value": "x64",
"strategy": "external"
},
"toolset": {
"value": "host=x64",
"strategy": "external"
}
}
],
"buildPresets": [
{
"name": "msvc-build",
"configurePreset": "msvc-x64-debug"
}
]
}

使用方法:

  1. 在 Windows 开始菜单中找到 “Developer Command Prompt for VS 2022” (或 “x64 Native Tools Command Prompt”)。

  2. 在这个特殊的黑窗口里切换到你的项目目录。

  3. 执行命令:

    1
    2
    cmake --preset msvc-x64-debug
    cmake --build --preset msvc-build

vs2022生成的CMakePresets.json

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "linux-debug",
"displayName": "Linux Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
},
{
"name": "macos-debug",
"displayName": "macOS Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
}
]
}

vs2022中下载intel编译器插件后兼容intel编译器写法

1
2
3
4
5
6
7
8
9
10
{
"name": "intel-preset",
"displayName": "Intel oneAPI (Standard)",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"toolset": "Intel C++ Compiler 2024",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},

使用继承的写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"version": 3,
"configurePresets": [
{
"name": "base-intel",
"hidden": true, // 基础模板,不直接出现在菜单里
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"toolset": "Intel C++ Compiler 2024"
},
{
"name": "intel-debug",
"inherits": "base-intel", // 继承上面的所有设置
"displayName": "Intel Debug",
"cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" }
},
{
"name": "intel-release",
"inherits": "base-intel", // 同样继承基础设置
"displayName": "Intel Release",
"cacheVariables": { "CMAKE_BUILD_TYPE": "Release" }
}
]
}

vs2022中使用qt6的生成内容

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
"version": 3,
"configurePresets": [
{
"name": "Qt-Debug",
"inherits": "Qt-Default",
"binaryDir": "${sourceDir}/out/build/debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_CXX_FLAGS": "-DQT_QML_DEBUG"
},
"environment": {
"QML_DEBUG_ARGS": "-qmljsdebugger=file:{95847481-d38e-4221-8acd-6147e1960ae3},block"
}
},
{
"name": "Qt-Release",
"inherits": "Qt-Default",
"binaryDir": "${sourceDir}/out/build/release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"hidden": true,
"name": "Qt-Default",
"inherits": "6.2.4_MSVC2022_x64_static_624_Release",
"vendor": {
"qt-project.org/Default": {
"checksum": "FsAWwLOKl2ZzQBHi+7pfzMzCCaE="
}
}
},
{
"hidden": true,
"name": "6.2.4_MSVC2022_x64_static_624_Release",
"inherits": "Qt",
"environment": {
"QTDIR": "C:/D/Program/Qt/6.2.4/MSVC2022_x64_static_624_Release"
},
"architecture": {
"strategy": "external",
"value": "x64"
},
"generator": "Ninja",
"vendor": {
"qt-project.org/Version": {
"checksum": "6oMTJK1lOtLDLC7g67vlM1aTh7s="
}
}
},
{
"hidden": true,
"name": "6.9.0_msvc2022_64",
"inherits": "Qt",
"environment": {
"QTDIR": "C:/D/Program/Qt/6.9.0/msvc2022_64"
},
"architecture": {
"strategy": "external",
"value": "x64"
},
"generator": "Ninja",
"vendor": {
"qt-project.org/Version": {
"checksum": "Reb5kZKUQyB8/QzLlsiPE61iZF8="
}
}
}
],
"vendor": {
"qt-project.org/Presets": {
"checksum": "Uiq1IXTmk1K08e3XdW2FG6sPumY="
}
}
}

编译命令

1
cmake --preset <预设名称>

clion中配置CMakePresets.json

clion中使用CMakePresets.json的前提

  1. 需要在ToolChains中添加system环境C:\Program Files (x86)\Intel\oneAPI\setvars.bat

  2. 设置编译器为icx.exe.

  3. 添加配置

    1
    2
    3
    4
    "vendor": {
    "jetbrains.com/clion": {
    "toolchain": "Intel oneAPI"
    }

完整配置:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
"version": 3,
"configurePresets": [
{
"name": "intel-icx-x64",
"displayName": "Intel C++ Compiler",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "icx.exe",
"CMAKE_CXX_COMPILER": "icx.exe",
"CMAKE_BUILD_TYPE": "Release"
},
"vendor": {
"jetbrains.com/clion": {
"toolchain": "Intel oneAPI"
}
}
},
{
"name": "gcc-x64",
"displayName": "GCC Compiler",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "gcc.exe",
"CMAKE_CXX_COMPILER": "g++.exe"
},
"vendor": {
"jetbrains.com/clion": {
"toolchain": "MinGW"
}
}
}
]
}