如何基于项目名全局设置CMake默认安装路径?
自定义CMake默认安装前缀(Windows 10)
我在Windows 10上频繁使用CMake构建自己或从网上获取的C++项目,希望修改install命令的默认前缀:
- 原默认路径:
C:/Program Files/${PROJECT_NAME} - 目标默认路径:
D:/ownbuilds/${PROJECT_NAME}(需自动带入项目名)
要求:
- 自动应用于所有新项目,除非项目自身CMakeLists指定了安装路径
- 无需修改项目原有的CMakeLists,也不用额外命令行选项(如
--prefix)
之前尝试利用CMake 3.29特性,将环境变量CMAKE_INSTALL_PREFIX设为D:/ownbuilds/${PROJECT_NAME},但${PROJECT_NAME}未被展开为实际项目名,实际安装路径变为D:/ownbuilds/bin/Toy.exe,而非期望的D:/ownbuilds/Toy/bin/Toy.exe。同时想明确:能否让CMake展开全局环境变量中的CMake变量(值随项目变化的变量)?
示例项目文件
toy/CMakeLists.txt:
cmake_minimum_required( VERSION 3.12...3.29 FATAL_ERROR ) project( Toy VERSION 1.0 ) add_executable( Toy main.cpp ) install( TARGETS Toy DESTINATION "bin" ) message( ">> CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
当前执行结果(环境变量设为D:/ownbuilds/${PROJECT_NAME})
C:\Users\User\Desktop\toy\build>cmake .. -- Building for: Visual Studio 17 2022 -- Selecting Windows SDK version 10.0.22621.0 to target Windows 10.0.19045. -- The C compiler identification is MSVC 19.41.34120.0 -- The CXX compiler identification is MSVC 19.41.34120.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.41.34120/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/2022/BuildTools/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done >> CMAKE_INSTALL_PREFIX = D:/ownbuilds/${PROJECT_NAME} -- Configuring done (6.4s) -- Generating done (0.0s) -- Build files have been written to: C:/Users/User/Desktop/toy/build C:\Users\User\Desktop\toy\build>cmake --build . --config Release MSBuild version 17.11.2+c078802d4 for .NET Framework 1>Checking Build System Building Custom Rule C:/Users/User/Desktop/toy/CMakeLists.txt main.cpp Toy.vcxproj -> C:\Users\User\Desktop\toy\build\Release\Toy.exe 'pwsh.exe' is not recognized as an internal or external command, operable program or batch file. Building Custom Rule C:/Users/User/Desktop/toy/CMakeLists.txt C:\Users\User\Desktop\toy\build>Release\Toy.exe Hello, world! C:\Users\User\Desktop\toy\build>cmake --install . --config Release -- Installing: D:/ownbuilds/bin/Toy.exe
期望结果
最后一行安装输出应为:
-- Installing: D:/ownbuilds/Toy/bin/Toy.exe
且Toy.exe实际安装到对应目录。
解决方案
核心说明
环境变量中的CMake变量不会自动展开,因为环境变量是系统级字符串,CMake不会对其进行变量插值。要实现需求,需通过CMake的全局配置机制实现。
方法1:使用用户级全局配置文件
CMake会自动加载Windows用户级配置文件%USERPROFILE%/.cmake/configure.cmd(若不存在则自行创建),在文件中添加以下内容:
if(NOT CMAKE_INSTALL_PREFIX) set(CMAKE_INSTALL_PREFIX "D:/ownbuilds/${PROJECT_NAME}" CACHE PATH "Default install path prefix" FORCE) endif()
- 逻辑:仅当项目未自行设置
CMAKE_INSTALL_PREFIX时,才应用自定义路径,${PROJECT_NAME}会被CMake正确展开为当前项目名 - 优先级:项目CMakeLists中的设置 > 全局配置文件设置 > 默认值
方法2:使用工具链文件(可选)
若需要更灵活的全局配置,可创建工具链文件(如my_cmake_toolchain.cmake):
if(NOT DEFINED CMAKE_INSTALL_PREFIX) set(CMAKE_INSTALL_PREFIX "D:/ownbuilds/${PROJECT_NAME}" CACHE PATH "Default install prefix" FORCE) endif()
然后设置系统环境变量CMAKE_TOOLCHAIN_FILE,指向该文件的绝对路径,CMake每次运行时会自动加载此文件。
验证效果
使用示例项目测试时:
- 执行
cmake ..后,message会输出>> CMAKE_INSTALL_PREFIX = D:/ownbuilds/Toy - 执行
cmake --install . --config Release时,程序会被安装到D:/ownbuilds/Toy/bin/Toy.exe,完全符合预期
内容的提问来源于stack exchange,提问作者Adayah
相关产品推荐
相关产品推荐

