Windows下add_custom_target编译报错:无法找到指定路径
问题
将应用从Linux移植到Windows时遇到编译错误,通过CMake构建mupdf Git子模块(该子模块用Makefile构建),CMake代码如下:
# Configure variables for building mupdf elseif(WIN32) set(MUPDF_OUTPUT "${PROJECT_SOURCE_DIR}/libs/mupdf/platform/win32/x64/Release/mupdfcpp64.lib" PARENT_SCOPE) set(MUPDF_OUTPUT "${PROJECT_SOURCE_DIR}/libs/mupdf/platform/win32/x64/Release/mupdfcpp64.lib") set(MUPDF_BUILD_COMMAND "python scripts/mupdfwrap.py --venv -b -j 0 m01") endif() add_custom_command( OUTPUT ${MUPDF_OUTPUT} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/mupdf COMMAND ${MUPDF_BUILD_COMMAND} COMMENT "Building mupdf (This takes a while) ..." BYPRODUCTS ${MUPDF_OUTPUT} ) add_custom_target(mupdf ALL DEPENDS ${MUPDF_OUTPUT} ) # Build add_library(application SHARED ${application_SRC} ) # Make sure to build mupdf before the application add_dependencies(application mupdf)
构建时输出错误:
-- Configuring done (0.7s) -- Generating done (0.5s) -- Build files have been written to: C:/Users/prtnp/Librum/build Change Dir: 'C:/Users/prtnp/Librum/build' Run Build Command(s): "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe" ALL_BUILD.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=16.0 /v:n Microsoft (R) Build Engine, version 16.11.2+f32259642 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. The build started on 07/22/2023 at 16:08:38. Project "C:\Users\prtnp\Librum\build\ALL_BUILD.vcxproj" on node "1" (default targets). The project "C:\Users\prtnp\Librum\build\ALL_BUILD.vcxproj" (1) is building "C:\Users\prtnp\Librum\build\ZERO_CHECK.vcxproj" (2) on node "1" (default targets). InitializeBuildStatus: Creating "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified. CustomBuild: All outputs are up-to-date. 1>Checking Build System FinalizeBuildStatus: Deleting file "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild". Touching "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate". The project "C:\Users\prtnp\Librum\build\ZERO_CHECK.vcxproj" is complete (default targets). The project "C:\Users\prtnp\Librum\build\ALL_BUILD.vcxproj" (1) is building "C:\Users\prtnp\Librum\build\src\application\mupdf.vcxproj" (3) on node "1" (default targets). InitializeBuildStatus: Touching "x64\Debug\mupdf\mupdf.tlog\unsuccessfulbuild". CustomBuild: "Building mupdf (This takes a while) ..." The system cannot find the path specified. C:\Program Files (x86)\Microsoft Visual Studio\2019/Community/MSBuild/Microsoft/VC/v160/Microsoft.CppCommon.targets(241,5): error MSB8066: The custom build for "C:\Users\prtnp\Librum\build\CMakeFiles\8e03d71809f012c9d6e5459fc00784b2\mupdfcpp64.lib.rule;C:\Users\prtnp\Librum\build\CMakeFiles\5736ecdae8f9a33915df7c26a6ac3c00\mupdf.rule" exited with code 3. [C:\Users\prtnp\Librum\build\src\application\mupdf.vcxproj] The project "C:\Users\prtnp\Librum\build\src\application\mupdf.vcxproj" is complete (default targets) -- ERROR. The project "C:\Users\prtnp\Librum\build\ALL_BUILD.vcxproj" is complete (default targets) -- ERROR. Build failed. "C:\Users\prtnp\Librum\build\ALL_BUILD.vcxproj" (default target) (1) -> "C:\Users\prtnp\Librum\build\src\application\mupdf.vcxproj" (default target) (3) -> (CustomBuild target) -> C:\Program Files (x86)\Microsoft Visual Studio\2019/Community/MSBuild/Microsoft/VC/v160/Microsoft.CppCommon.targets(241,5): error MSB8066: The custom build for "C:\Users\prtnp\Librum\build\CMakeFiles\8e03d71809f012c9d6e5459fc00784b2\mupdfcpp64.lib.rule;C:\Users\prtnp\Librum\build\CMakeFiles\5736ecdae8f9a33915df7c26a6ac3c00\mupdf.rule" exited with code 3. [C:\Users\prtnp\Librum\build\src\application\mupdf.vcxproj] 0 Warning(s) 1 Error(s) Time Elapsed 00:00:01.28
手动在指定工作目录执行${MUPDF_BUILD_COMMAND}可正常运行,CMake命令为:
cmake -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=C:\\Qt\\6.5.2\\msvc2019_64 ..
可能的原因及解决办法
命令参数解析问题:Windows下CMake如果用字符串包裹整个命令,可能无法正确拆分参数。将
MUPDF_BUILD_COMMAND改为列表形式,去掉外层引号:set(MUPDF_BUILD_COMMAND python scripts/mupdfwrap.py --venv -b -j 0 m01)让CMake自动处理参数拆分,避免空格或特殊字符导致的路径识别错误。
Python路径环境问题:手动执行时环境变量能找到Python,但MSBuild运行时可能未加载完整用户环境。指定Python绝对路径:
set(MUPDF_BUILD_COMMAND "C:/Python39/python.exe" scripts/mupdfwrap.py --venv -b -j 0 m01)替换为你实际的Python安装路径。
构建配置不匹配:当前CMake指定
Debug配置,但MUPDF_OUTPUT指向Release目录,导致MSBuild构建时路径不匹配。根据CMAKE_BUILD_TYPE动态设置输出路径:if(CMAKE_BUILD_TYPE STREQUAL "Debug") set(MUPDF_OUTPUT "${PROJECT_SOURCE_DIR}/libs/mupdf/platform/win32/x64/Debug/mupdfcpp64.lib") else() set(MUPDF_OUTPUT "${PROJECT_SOURCE_DIR}/libs/mupdf/platform/win32/x64/Release/mupdfcpp64.lib") endif() set(MUPDF_OUTPUT "${PROJECT_SOURCE_DIR}/libs/mupdf/platform/win32/x64/${CMAKE_BUILD_TYPE}/mupdfcpp64.lib" PARENT_SCOPE)工作目录验证:添加日志确认工作目录是否正确:
message(STATUS "MUPDF working directory: ${PROJECT_SOURCE_DIR}/libs/mupdf")检查CMake配置阶段输出的路径是否存在。
内容的提问来源于stack exchange,提问作者Creapermann
相关产品推荐
相关产品推荐

