CMAKE_GENERATOR_TOOLSET and CMAKE_TOOLCHAIN_FILE

V3.1.3:

CMAKE_GENERATOR_TOOLSET

Native build system toolset name specified by user.

Some CMake generators support a toolset name to be given to the native build system to choose a compiler. If the user specifies a toolset name (e.g. via the cmake -T option) the value will be available in this variable.

The value of this variable should never be modified by project code. A toolchain file specified by the CMAKE_TOOLCHAIN_FILE variable may initialize CMAKE_GENERATOR_TOOLSET. Once a given build tree has been initialized with a particular value for this variable, changing the value has undefined behavior.

CMAKE_TOOLCHAIN_FILE

Path to toolchain file supplied to cmake(1).

This variable is specified on the command line when cross-compiling with CMake. It is the path to a file which is read early in the CMake run and which specifies locations for compilers and toolchain utilities, and other target platform and compiler related information.

Set Non-Default Platform Toolset in CMake

In general, CMake uses the default platform toolset of the generator. The generator mandates the format of the project files. If the user chooses Visual Studio 11 (Microsoft Visual Studio 2012) as the generator, CMake generates the project files that VS 2012 can load. At the same time, in the project files, the compiler version is automatically set to v110, i.e., Microsoft Visual C++ 11.0 (_MSC_VER 1700).

There are chances that the default platform toolset is not appropriate. For example, a VS2012 project may be configured by the user to be built with different platform toolsets, such as v90(Visual Studio 2008), v100(Visual Studio 2010), v110_xp(Visual Studio 2012 – Windows XP), and lately, v120_CTP_Nov2012(Microsoft Visual C++ Compiler Nov 2012 CTP). Specifically, projects using certain C++11 features, such as variadic template, have to be compiled with v120_CTP_Nov2012 in Visual Studio 2012, because v110 does not support those features.

The CMake team saw the need and enabled CMake users to change the platform toolset in a generator, see bugs 0010722: Add PlatformToolset support to Visual Studio 2010 generator and 0013774: Support v110_xp target with MSVC 2012 Update 1. The latest CMake 2.8.11 Reference mentioned in a few occasions about toolset:

I prefer to capture the project settings in CMakeLists.txt than a command line option, so I skip -T option. I tried both CMAKE_GENERATOR_TOOLSET andCMAKE_VS_PLATFORM_TOOLSET, and I had better luck with CMAKE_GENERATOR_TOOLSET. Below is a CMakeLists.txt file using the CTP compiler instead:

cmake_minimum_required (VERSION 2.8)

set ( MY_MACRO_INTEGER 42 ) # see configure file below

project (test_sln)  # VS solution

if(MSVC OR MSVC_IDE) 
  if( MSVC_VERSION LESS 1700 )       # VC10-/VS2010- 
    message(FATAL_ERROR "The project requires C++11 features. " 
      "You need at least Visual Studio 11 (Microsoft Visual Studio 2012), " 
      "with Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)."

  elseif( MSVC_VERSION EQUAL 1700 )  # VC11/VS2012 
    message( "VC11: use Microsoft Visual Studio 2012 " 
      "with Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)"
 ) 
    set(CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012" CACHE STRING "Platform Toolset" FORCE) 
  else() # VC12+, assuming C++11 supported. 
  endif() 
endif()

configure_file ( 
  "${PROJECT_SOURCE_DIR}/test.h.in" 
  "${PROJECT_SOURCE_DIR}/test.h" 
)

set ( ConfigFiles 
  test.h.in 
)

set ( GeneratedFiles 
  test.h 
)

source_group( "config" FILES ${ConfigFiles} ) 
source_group( "generated" FILES ${GeneratedFiles} )

add_executable(test_proj test.cpp ${ConfigFiles} ${GeneratedFiles})  # VS project

The CMake file basically detects if MSVC is the generator, and when VC11 is used it changes the platform toolset to v120_CTP_Nov2012.

I have to click Configure twice to clear the red variables in CMake GUI before I can generate the correct project files.

Sometimes when the project files are already generated, but there is some changes in the CMakeLists.txt files and you regenerate the project files, CMake goes wild. For example, I received this error a few times:

CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message): 
The C compiler "C:/Program Files (x86)/Microsoft Visual C++ Compiler Nov 
2012 CTP/bin/cl.exe" is not able to compile a simple test program. 
It fails with the following output: 
Change Dir: C:/Temp/cmake_test/build/CMakeFiles/CMakeTmp 
Run Build Command:C:\PROGRA~2\MICROS~3.0\Common7\IDE\devenv.com 
CMAKE_TRY_COMPILE.sln /build Debug /project cmTryCompileExec3629620200 

I have to close CMake GUI and remove the out-of-source build directory entirely, then re-generate the project files from scratch to overcome this problem.

For this reason, I recommend using the out-of-source build directory for easier start-over.

Filter in Project in Visual Studio

The CMake file above also shows source_groups, which are used to organize the files in a project under filters in Visual Studio. In the screenshot below, you can find test.h.in under Filter config, and test.h under Filter generated. Do not forget to add the corresponding files to the project (test_proj here), otherwise this does not work.

Filters are handy. In this example, I know I should never try to modify test.h in Visual Studio because it is generated.


Configure Files

The last thing the CMake file shows is the configure_file feature of CMake. CMake basically uses test.h.in as a template and generates test.h along with project files. This is test.h.in:

#define MY_MACRO_INTEGER @MY_MACRO_INTEGER@ 
#define CMAKE_VS_PLATFORM_TOOLSET "@CMAKE_VS_PLATFORM_TOOLSET@" 
#define CMAKE_GENERATOR_TOOLSET "@CMAKE_GENERATOR_TOOLSET@"

CMake replaces @VARIABLE_NAME@ with the actual variable value in the configure file, then generates the result file test.h

#define MY_MACRO_INTEGER 42 
#define CMAKE_VS_PLATFORM_TOOLSET "v120_CTP_Nov2012" 
#define CMAKE_GENERATOR_TOOLSET "v120_CTP_Nov2012"

As you can see, it is able to access both my custom variable and CMake built-in variables. If you only use the CMake variables as macros, you can use #cmakedefine in the configure file. Or use CMake command add_definitions to add preprocessor macros.

Other Reference

three_cpp uses another approach to set the platform toolset. It uses the CMake set_target_properties command to set the build target’s PLATFORM_TOOLSET property to for example, v120_CTP_Nov2012, see ThreeTargets.cmake and CMakeLists.txt. This does not work for me on CMake 2.8.11, and I do not find in CMake 2.8.11 documentation about this property either.



http://www.cmake.org/cmake/help/v3.1/variable/CMAKE_GENERATOR_TOOLSET.html

http://binglongx.com/2013/06/28/set-non-default-platform-toolset-in-cmake/


<think>嗯,用户的问题是关于CMake中如何配置x86和x64构建目标。之前提供的站内引用已经给出了一些线索,需要整理成系统化的方法。用户引用了四段材料,其中[1]和[2]明确提到使用VisualStudio生成器时通过-G参数指定架构,[4]展示了配置文件的写法。看来用户主要在Windows平台工作,可能使用VS作为编译器。用户的核心诉求是“切换或修改构建目标”,这需要从两个层面考虑:配置阶段的生成器选择(决定架构)和构建阶段的参数传递(决定实际编译版本)。需要特别注意三点:1平台选择的关键在于-G参数的后缀(是否有Win64)2不同CMake版本对架构的支持方式可能有差异3对于多配置生成器(如VS)还需要注意--config参数用户可能遇到的典型场景包括:-从x86切换到x64时忘记清理缓存导致架构冲突-使用Ninja等单配置生成器时未正确指定toolchain-配置文件中未明确定义两个架构的构建选项准备在回复中分场景说明,并强调清理缓存的重要性,因为这是实际开发中最容易踩的坑。对于跨平台需求,toolchain文件的方式更通用,但用户当前集中在Windows平台,所以把VS生成器的说明放在最前面。用户没有提到CMakeLists.txt的修改,说明问题集中在配置层面而非代码层面,所以无需讨论CMAKE_SYSTEM_PROCESSOR等变量。但需要补充命令行验证生成结果的方法,帮助用户确认配置是否生效。</think> 在CMake中配置x86和x64构建目标的方法主要有以下三种,具体操作根据生成器类型和平台有所不同: --- ### 一、Windows平台(Visual Studio生成器) #### 1. 通过生成器指定架构 ```bash # x64架构 cmake -G "Visual Studio 17 2022" -A x64 -B build # x86架构 cmake -G "Visual Studio 17 2022" -A Win32 -B build ``` - **关键参数**: - `-A x64`:指定64位目标 - `-A Win32`:指定32位目标 - `-B build`:指定构建目录[^1][^2] #### 2. 旧版VS指定方法(CMake <3.15) ```bash # VS2019 x64 cmake -G "Visual Studio 16 2019 Win64" # VS2019 x86 cmake -G "Visual Studio 16 2019" ``` --- ### 二、通用方法(所有平台) #### 1. 工具链文件(推荐跨平台方案) 创建`x86-toolchain.cmake`: ```cmake # 工具链文件内容 set(CMAKE_GENERATOR_PLATFORM Win32) # 或 x64 set(CMAKE_GENERATOR_TOOLSET "v143") # 指定VS工具集 ``` 使用命令: ```bash cmake -DCMAKE_TOOLCHAIN_FILE=x86-toolchain.cmake -B build ``` #### 2. 预设配置文件(CMake ≥3.19) 在`CMakePresets.json`中配置: ```json { "configurePresets": [ { "name": "win64-release", "generator": "Visual Studio 17 2022", "architecture": "x64", "configurationType": "Release" }, { "name": "win32-debug", "generator": "Visual Studio 17 2022", "architecture": "Win32", "configurationType": "Debug" } ] } ``` 调用命令: ```bash cmake --preset=win64-release # 启动x64构建[^4] ``` --- ### 三、构建阶段指定(补充方法) ```bash # 指定Release配置的x64目标 cmake --build build --config Release --target ALL_BUILD -- /p:Platform=x64 # Ninja生成器指定架构 cmake -G Ninja -DCMAKE_CXX_FLAGS="-m32" # x86 cmake -G Ninja -DCMAKE_CXX_FLAGS="-m64" # x64 ``` --- ### ▎关键注意事项 1. **缓存清理**:切换架构前务必删除`CMakeCache.txt`或整个`build`目录 2. **架构验证**:生成后检查日志确认架构 ```log -- Building for: Visual Studio 17 2022 -- Selected Windows SDK version: 10.0.19041.0 -- The CXX compiler identification: MSVC 19.30.30709.0 -- **Platform: x64** << 关键确认点 ``` 3. **多目标管理**:通过`CMAKE_CONFIGURATION_TYPES`支持同时生成多架构 ```cmake set(CMAKE_CONFIGURATION_TYPES "Debug;Release;x86_Debug;x64_Release") ``` --- > **实际应用场景**: > 若需同时支持x86/x64安装包,可在CI流水线中并行执行: > ```yml > steps: > - name: Build x86 > run: cmake -G "VS2022" -A Win32 -B build_x86 > - name: Build x64 > run: cmake -G "VS2022" -A x64 -B build_x64 > ``` > 此方法常见于Windows平台的混合架构交付场景[^3][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值