我对CMake处理Windows机器上的库链接的方式感到有些困惑。下面有一个简单的CMakeLists.txt
文件:
cmake_minimum_required (VERSION 3.10)
project (sample)
add_library (quad SHARED quad.cpp)
add_executable (run main.cpp)
target_link_libraries (run PRIVATE quad)
在Linux / Mac计算机上,CMake可以正确构建* .so / *。dylib库,并将可执行文件链接到* .so / *。dylib文件。但是,在Windows计算机上,它将生成* .dll文件,然后尝试链接到* .lib文件。
引擎盖下的CMake执行以下命令:
>------ Build started: Project: CMakeLists, Configuration: Debug ------
[1/4] C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\HostX64\x64\cl.exe /nologo /TP -Dquad_EXPORTS /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoCMakeFiles\quad.dir\quad.cpp.obj /FdCMakeFiles\quad.dir\ /FS -c C:\Users\UX\Workspace\sample\quad.cpp
[2/4] C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\HostX64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoCMakeFiles\run.dir\main.cpp.obj /FdCMakeFiles\run.dir\ /FS -c C:\Users\UX\Workspace\sample\main.cpp
[3/4] cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_dll --intdir=CMakeFiles\quad.dir --manifests -- C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe /nologo CMakeFiles\quad.dir\quad.cpp.obj /out:quad.dll /implib:quad.lib /pdb:quad.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
[4/4] cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\run.dir --manifests -- C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe /nologo CMakeFiles\run.dir\main.cpp.obj /out:run.exe /implib:run.lib /pdb:run.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console quad.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
FAILED: run.exe
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\run.dir --manifests -- C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe /nologo CMakeFiles\run.dir\main.cpp.obj /out:run.exe /implib:run.lib /pdb:run.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console quad.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe /nologo CMakeFiles\run.dir\main.cpp.obj /out:run.exe /implib:run.lib /pdb:run.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console quad.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\run.dir/intermediate.manifest CMakeFiles\run.dir/manifest.res" failed (exit code 1104) with the following output:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\LINK : fatal error LNK1104: cannot open file 'quad.lib'
ninja: build stopped: subcommand failed.
Build failed.
我的问题是,当CMake文档声明以下内容时,为什么它在Windows上失败:
target_link_libraries(<target>
<PRIVATE|PUBLIC|INTERFACE> <item>...
[<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
item
在哪里A library target name
?它不应该从target
的属性中获得足够的信息来标识已编译库的完整路径及其类型(即SHARED
)以能够正确链接可执行文件吗? >
答案 0 :(得分:0)
这是Windows中链接的工作方式。您需要一个foo.lib
文件才能链接到foo.dll
库。当.lib
本身链接时产生此.dll
。您必须分发.lib
文件和库头。