Windows下CMake SuperBuild通过ExternalProject_Add下载Boost报错解决
报错含义
错误码9009是Windows环境下找不到对应可执行程序的标准报错,核心触发原因是你当前在Windows + Visual Studio环境下构建,但CMake配置里写死了仅适用于Linux/macOS的shell命令,Windows命令提示符无法识别:
- 报错明确提示
'cp' is not recognized as an internal or external command:cp是类Unix系统的文件复制命令,Windows cmd对应命令为copy - 配置中调用的
bootstrap.sh是类Unix平台的Boost引导脚本,Windows平台对应的引导脚本是bootstrap.bat - 配置中写的
./b2是类Unix平台执行当前目录二进制的语法,Windows平台直接调用b2.exe即可,不需要./前缀 - 自定义构建步骤因为找不到上述命令直接终止,最终触发MSB8066构建失败报错。
修复方案
你当前使用的配置是面向类Unix平台的,需要增加平台判断逻辑,替换为Windows兼容的命令,具体修改如下:
- 替换原有硬编码的Boost命令配置,根据构建平台自动选择对应命令:
删除原有这两行配置:
set( Boost_Bootstrap_Command ${CMAKE_BINARY_DIR}/boost-download/src/boost-external/bootstrap.sh && cp ${CMAKE_BINARY_DIR}/boost-download/src/boost-external-build/b2 ${CMAKE_BINARY_DIR}/boost-download/src/boost-external/) set( Boost_b2_Command cd ${CMAKE_BINARY_DIR}/boost-download/src/boost-external && ./b2 )
替换为跨平台兼容的配置:
if(WIN32) # Windows平台命令 set(Boost_Bootstrap_Command bootstrap.bat) set(Boost_b2_Command b2.exe) else() # Linux/macOS平台命令 set(Boost_Bootstrap_Command ./bootstrap.sh && cp ./b2 ./) set(Boost_b2_Command ./b2) endif()
- 修改
ExternalProject_Add配置,增加BUILD_IN_SOURCE 1参数,让CMake直接在Boost源码目录内执行配置和构建命令,不需要手动写cd切换路径,避免跨平台路径拼接错误,修改后的完整配置如下:
ExternalProject_Add( boost-external GIT_REPOSITORY https://github.com/boostorg/boost.git GIT_TAG boost-1.79.0 PREFIX boost-download BUILD_IN_SOURCE 1 CONFIGURE_COMMAND ${Boost_Bootstrap_Command} BUILD_COMMAND ${Boost_b2_Command} install --without-python --without-mpi --disable-icu --prefix=${CMAKE_SOURCE_DIR}/thirdparty/boost/ --threading=single,multi --link=shared --variant=release -j12 INSTALL_COMMAND "" )
- 额外注意事项:
- Windows环境下必须从Visual Studio开发者命令提示符中执行CMake配置和构建命令,否则
bootstrap.bat会找不到MSVC编译器,导致Boost引导失败 - Windows下运行
bootstrap.bat后会直接在源码目录生成b2.exe,不需要额外执行复制操作,原有配置里的cp逻辑在Windows下完全多余 - 原有配置中
include_directories(boostdemo ${Boost_INCLUDE_DIR})写法错误,include_directories是全局添加头文件目录的命令,不需要传入目标名,建议替换为target_include_directories(boostdemo PRIVATE ${Boost_INCLUDE_DIR}),仅为当前目标添加头文件路径,避免全局配置污染。
原始报错信息
Performing update step for 'boost-external' No patch step for 'boost-external' Performing configure step for 'boost-external' 'cp' is not recognized as an internal or external command, operable program or batch file. C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Msbuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(241,5): error MSB8066: Custom build for 'C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-mkdir.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-download.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-update.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-patch.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-configure.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-build.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\7a303cde4719f69085ebded4413f3304\boost-external-install.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\1a2a6d9ea64ba592f97cd191c1d5acb1\boost-external-complete.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\CMakeFiles\a70b522e33d20f4a0fb02fd1b3fdb173\boost-external.rule;C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\CMakeLists.txt' exited with code 9009. [C:\IBOIS57\_Code\Software\CPP\CMAKE\super_build\Example_02_Boost\build\boost-external.vcxproj]

内容的提问来源于stack exchange,提问作者PetrasVestartasEPFL
相关产品推荐
相关产品推荐

