CMake 3.8.0在生成文件中生成错误的链接命令 [英] CMake 3.8.0 generates wrong link command in makefiles
问题描述
我运行cmake生成一个带有STATIC库的项目,该项目成功完成后,ninja和mingw32-make都无法使其目标链接.对于共享库或可执行文件,此相同设置可以正常工作.我已经为忍者"和"MinGW Makefiles"生成器尝试过:
After I run cmake to generate a project with a STATIC library, which completes successfully, both ninja and mingw32-make fail to make their targets at linking. For SHARED libraries or executables this same setup worked fine. I've tried this both for "Ninja" and "MinGW Makefiles" generators:
忍者输出:
[2/2] Linking CXX static library hello_wsl.lib
FAILED: hello_wsl.lib
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E remove hello_wsl.lib && "" qc hello_wsl.lib CMakeFiles/hello_wsl.dir/lib_hello_world.cpp.obj && cd ."
"""" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.
ninja: build stopped: subcommand failed.
mingw32-make输出:
mingw32-make output:
Scanning dependencies of target hello_wsl
[ 50%] Building CXX object CMakeFiles/hello_wsl.dir/lib_hello_world.cpp.obj
[100%] Linking CXX static library hello_wsl.lib
Error running link command: El parámetro no es correcto
CMakeFiles\hello_wsl.dir\build.make:93: recipe for target 'hello_wsl.lib' failed
mingw32-make.exe[2]: *** [hello_wsl.lib] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/hello_wsl.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/hello_wsl.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2
还要看看由"MinGW Makefiles"生成的"project_root \ build \ CMakeFiles \ hello_wsl.dir \ link.txt":
Also take a look at "project_root\build\CMakeFiles\hello_wsl.dir\link.txt" generated by "MinGW Makefiles":
"" qc hello_wsl.lib CMakeFiles/hello_wsl.dir/lib_hello_world.cpp.obj
分步示例项目,以重现"MinGW Makefiles"的问题:
@ECHO OFF
SETLOCAL
@ECHO OFF
MOVE "%~dp0\build\CMakeCache.txt" ".\"
RMDIR "%~dp0\build" /S /Q
MKDIR "%~dp0\build"
MOVE "%~dp0\CMakeCache.txt" ".\build\"
ENDLOCAL
重现错误后,打开CMD并运行:
After reproducing the error, open CMD and run:
project_root\fix.bat
project_root\configure.bat
project_root\build.bat
忍者输出(成功链接):
ninja output after fix (successfully linked):
[2/2] Linking CXX static library hello_wsl.lib
mingw32-make修复后(成功链接):
mingw32-make after fix (successfully linked):
Scanning dependencies of target hello_wsl
[ 50%] Building CXX object CMakeFiles/hello_wsl.dir/lib_hello_world.cpp.obj
[100%] Linking CXX static library hello_wsl.lib
[100%] Built target hello_wsl
-
在应用修订并重新运行配置和构建之前,我对CMakeCache.txt(CMakeCache.txt.before_fix)进行了备份.重新配置并成功构建项目后,CMakeCache.txt.before_fix和CMakeCache.txt文件之间没有区别.
I made a backup of CMakeCache.txt (CMakeCache.txt.before_fix) before applying the fix and reruning configure and build. There was no diference between the CMakeCache.txt.before_fix and CMakeCache.txt files after reconfiguring and successfully building the project.
我还备份了"rules.ninja",然后将其与FC进行了比较.这是FC"project_root \ rules.ninja.before_fix""project_root \ build \ rules.ninja"的输出:
I also made a backup of "rules.ninja" and then compared them with FC. This is the output of FC "project_root\rules.ninja.before_fix" "project_root\build\rules.ninja":
Comparando archivos .\rules.ninja.before_fix y .\BUILD\RULES.NINJA
***** .\rules.ninja.before_fix
rule CXX_STATIC_LIBRARY_LINKER__lib_hello_world
command = cmd.exe /C "$PRE_LINK && "C:\Program Files\CMake\bin\cmake.exe" -E remove $TARGET_FILE && "" qc $TARGET_FILE $LINK_
FLAGS $in && $POST_BUILD"
description = Linking CXX static library $TARGET_FILE
***** .\BUILD\RULES.NINJA
rule CXX_STATIC_LIBRARY_LINKER__lib_hello_world
command = cmd.exe /C "$PRE_LINK && "C:\Program Files\CMake\bin\cmake.exe" -E remove $TARGET_FILE && C:\PROGRA~1\MinGWx64\bin\
ar.exe qc $TARGET_FILE $LINK_FLAGS $in && C:\PROGRA~1\MinGWx64\bin\ranlib.exe $TARGET_FILE && $POST_BUILD"
description = Linking CXX static library $TARGET_FILE
*****
并对"MinGW Makefiles"执行相同的操作.这是FC"project_root \ link.txt.before_fix""project_root \ build \ CMakeFiles \ lib_hello_world.dir \ link.txt"的输出:
And did the same for "MinGW Makefiles". This is the output of FC "project_root\link.txt.before_fix" "project_root\build\CMakeFiles\lib_hello_world.dir\link.txt":
Comparando archivos .\link.txt.before_fix y .\BUILD\CMAKEFILES\LIB_HELLO_WORLD.DIR\LINK.TXT
***** .\link.txt.before_fix
"" qc hello_wsl.lib CMakeFiles/hello_wsl.dir/lib_hello_world.cpp.obj
***** .\BUILD\CMAKEFILES\LIB_HELLO_WORLD.DIR\LINK.TXT
C:\PROGRA~1\MinGWx64\bin\ar.exe qc lib_hello_world.lib CMakeFiles/lib_hello_world.dir/lib_hello_world.cpp.obj
C:\PROGRA~1\MinGWx64\bin\ranlib.exe lib_hello_world.lib
*****
推荐答案
有关 CMAKE_AR 和 CMAKE_RUNLIB 变量的一些具体说明:根据
There is some specific about CMAKE_AR and CMAKE_RUNLIB variables: according to that bugreport they should be declared as CACHED:
SET(CMAKE_AR "$ENV{MINGW_W64_BIN_DIR}/ar.exe" CACHE FILEPATH "Arhiver")
SET(CMAKE_RANLIB "$ENV{MINGW_W64_BIN_DIR}/ranlib.exe" CACHE FILEPATH "Runlib")
这篇关于CMake 3.8.0在生成文件中生成错误的链接命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!