我可以通过设置工具链文件来替换调用 cmake -G<generator> 选项和调用 vcvarsall.bat 吗?
Can I replace calling the cmake -G<generator> option and calling vcvarsall.bat by setting a toolchain file?
当使用 CMake 为 64 位架构生成 Visual Studio15 解决方案时,必须先调用 vcvarsall.bat amd64
,然后使用生成器选项 cmake . -Bbuild -G"Visual Studio 14 2015 Win64"
调用 cmake。 CMake 将在执行 project()
函数时确定几个变量的值。
CMAKE_GENERATOR: Visual Studio 14 2015 Win64
CMAKE_BUILD_TOOL: C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
CMAKE_CXX_COMPILER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
CMAKE_C_COMPILER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
CMAKE_LINKER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/link.exe
CMAKE_CXX_COMPILER_ID: MSVC
CMAKE_CXX_COMPILER_VERSION: 19.0.24215.1
CMAKE_VS_PLATFORM_NAME: x64
我想通过在工具链文件中设置变量值来摆脱对 vcvarsall.bat
和 -G"generator"
选项的调用,如下所示:
# VisualStudio2015.cmake
set(CMAKE_GENERATOR "Visual Studio 14 2015 Win64" CACHE STRING "The CMake generator" FORCE )
set(CMAKE_BUILD_TOOL "C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe" CACHE FILEPATH "The visual studio build-system" FORCE)
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_LINKER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/link.exe" CACHE FILEPATH "Microsoft linker" FORCE)
set(CMAKE_CXX_COMPILER_ID MSCV CACHE STRING "The Id string of the compiler" FORCE)
set(CMAKE_CXX_COMPILER_VERSION 19.0.24215.1 CACHE STRING "The version of the compiler" FORCE)
set(CMAKE_VS_PLATFORM_NAME x64 CACHE STRING "Target processor architecture" FORCE)
然后使用 CMAKE_TOOLCHAIN_FILE
选项调用 cmake:
cmake . -Bbuild -DCMAKE_TOOLCHAIN_FILE=VisualStudio2015.cmake
问题是这似乎不起作用。当 cmake 执行 project()
函数时,它会覆盖我为编译器设置的值。那么我只是忘记设置一些必需的变量还是这根本不可能?
感谢您的宝贵时间。
将我的评论变成答案
我认为您正在寻找的不是工具链文件(因为您无法将 CMAKE_GENERATOR
there), but the -C <initial-cache>
选项更改为 "Pre-load a script to populate the cache"。
而且你不应该在脚本中放入太多 CMake 通常 detects/evaluates 的值。
所以我已经成功测试了以下内容:
VS2015InitialCache.cmake
set(CMAKE_GENERATOR "Visual Studio 14 2015 Win64" CACHE STRING "The CMake generator" FORCE)
set(CMAKE_CACHE_INIT_FILE "${CMAKE_CURRENT_LIST_FILE}" CACHE STRING "The CMake cache init file used" FORCE)
set(CMAKE_MAKE_PROGRAM "C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/devenv.com" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft CXX compiler" FORCE)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft C compiler" FORCE)
然后你打电话:
> cmake -CVS2015InitialCache.cmake -H. -Bbuild
loading initial cache file VS2015InitialCache.cmake
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
...
参考资料
- CMake preload script for cache
当使用 CMake 为 64 位架构生成 Visual Studio15 解决方案时,必须先调用 vcvarsall.bat amd64
,然后使用生成器选项 cmake . -Bbuild -G"Visual Studio 14 2015 Win64"
调用 cmake。 CMake 将在执行 project()
函数时确定几个变量的值。
CMAKE_GENERATOR: Visual Studio 14 2015 Win64
CMAKE_BUILD_TOOL: C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
CMAKE_CXX_COMPILER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
CMAKE_C_COMPILER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
CMAKE_LINKER: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/link.exe
CMAKE_CXX_COMPILER_ID: MSVC
CMAKE_CXX_COMPILER_VERSION: 19.0.24215.1
CMAKE_VS_PLATFORM_NAME: x64
我想通过在工具链文件中设置变量值来摆脱对 vcvarsall.bat
和 -G"generator"
选项的调用,如下所示:
# VisualStudio2015.cmake
set(CMAKE_GENERATOR "Visual Studio 14 2015 Win64" CACHE STRING "The CMake generator" FORCE )
set(CMAKE_BUILD_TOOL "C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe" CACHE FILEPATH "The visual studio build-system" FORCE)
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_LINKER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/link.exe" CACHE FILEPATH "Microsoft linker" FORCE)
set(CMAKE_CXX_COMPILER_ID MSCV CACHE STRING "The Id string of the compiler" FORCE)
set(CMAKE_CXX_COMPILER_VERSION 19.0.24215.1 CACHE STRING "The version of the compiler" FORCE)
set(CMAKE_VS_PLATFORM_NAME x64 CACHE STRING "Target processor architecture" FORCE)
然后使用 CMAKE_TOOLCHAIN_FILE
选项调用 cmake:
cmake . -Bbuild -DCMAKE_TOOLCHAIN_FILE=VisualStudio2015.cmake
问题是这似乎不起作用。当 cmake 执行 project()
函数时,它会覆盖我为编译器设置的值。那么我只是忘记设置一些必需的变量还是这根本不可能?
感谢您的宝贵时间。
将我的评论变成答案
我认为您正在寻找的不是工具链文件(因为您无法将 CMAKE_GENERATOR
there), but the -C <initial-cache>
选项更改为 "Pre-load a script to populate the cache"。
而且你不应该在脚本中放入太多 CMake 通常 detects/evaluates 的值。
所以我已经成功测试了以下内容:
VS2015InitialCache.cmake
set(CMAKE_GENERATOR "Visual Studio 14 2015 Win64" CACHE STRING "The CMake generator" FORCE)
set(CMAKE_CACHE_INIT_FILE "${CMAKE_CURRENT_LIST_FILE}" CACHE STRING "The CMake cache init file used" FORCE)
set(CMAKE_MAKE_PROGRAM "C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/devenv.com" CACHE FILEPATH "Microsoft compiler" FORCE)
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft CXX compiler" FORCE)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe" CACHE FILEPATH "Microsoft C compiler" FORCE)
然后你打电话:
> cmake -CVS2015InitialCache.cmake -H. -Bbuild
loading initial cache file VS2015InitialCache.cmake
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
...
参考资料
- CMake preload script for cache