繁体   English   中英

将Qt应用程序与CMake和Visual Studio链接

[英]Linking Qt app with CMake and Visual Studio

我正在尝试使用CMake和Visual Studio 2019构建一个简单的测试Qt应用程序。

我将基于此示例进行测试: https : //github.com/jasondegraw/Qt-CMake-HelloWorld

我使用Visual Studio打开了CMakeLists.txt文件。 在构建过程中,我得到:

[7/7] cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\helloworld.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x86\mt.exe --manifests  -- C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx86\x64\link.exe /nologo CMakeFiles\helloworld.dir\helloworld_autogen\mocs_compilation.cpp.obj CMakeFiles\helloworld.dir\main.cpp.obj CMakeFiles\helloworld.dir\mainwindow.cpp.obj CMakeFiles\helloworld.dir\helloworld_autogen\EWIEGA46WW\qrc_resources.cpp.obj  /out:helloworld.exe /implib:helloworld.lib /pdb:helloworld.pdb /version:0.0  /machine:x64 /debug /INCREMENTAL /subsystem:windows  D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Widgetsd.a D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Guid.a D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Cored.a D:\Program\Qt\5.13.1\mingw73_64\lib\libqtmaind.a kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
  FAILED: helloworld.exe 
  cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\helloworld.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x86\mt.exe --manifests  -- C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx86\x64\link.exe /nologo CMakeFiles\helloworld.dir\helloworld_autogen\mocs_compilation.cpp.obj CMakeFiles\helloworld.dir\main.cpp.obj CMakeFiles\helloworld.dir\mainwindow.cpp.obj CMakeFiles\helloworld.dir\helloworld_autogen\EWIEGA46WW\qrc_resources.cpp.obj  /out:helloworld.exe /implib:helloworld.lib /pdb:helloworld.pdb /version:0.0  /machine:x64 /debug /INCREMENTAL /subsystem:windows  D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Widgetsd.a D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Guid.a D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Cored.a D:\Program\Qt\5.13.1\mingw73_64\lib\libqtmaind.a 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~2\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx86\x64\link.exe /nologo CMakeFiles\helloworld.dir\helloworld_autogen\mocs_compilation.cpp.obj CMakeFiles\helloworld.dir\main.cpp.obj CMakeFiles\helloworld.dir\mainwindow.cpp.obj CMakeFiles\helloworld.dir\helloworld_autogen\EWIEGA46WW\qrc_resources.cpp.obj /out:helloworld.exe /implib:helloworld.lib /pdb:helloworld.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:windows D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Widgetsd.a D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Guid.a D:\Program\Qt\5.13.1\mingw73_64\lib\libQt5Cored.a D:\Program\Qt\5.13.1\mingw73_64\lib\libqtmaind.a kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\helloworld.dir/intermediate.manifest CMakeFiles\helloworld.dir/manifest.res" failed (exit code 1120) with the following output:
D:\Download\mocs_compilation.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: struct QMetaObject * __cdecl QObjectData::dynamicMetaObject(void)const " (__imp_?dynamicMetaObject@QObjectData@@QEBAPEAUQMetaObject@@XZ) referenced in function "public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void)const " (?metaObject@MainWindow@@UEBAPEBUQMetaObject@@XZ)

之后会加载更多LNK2019错误。 唯一不同的是,我在CMakeLists.txt中注释掉了“ -Wall”行。

据我所知,此示例具有所有必需的元素,可以正常工作。

我唯一指出的是我安装了Qt 5.13.1 msvc2017并且正在运行msvc2019,但是Qt论坛上的一个线程指出这两个版本是二进制兼容的。

对这个问题有什么想法吗?

更新:正如@Tsyvarev指出的那样,当Visual Studio运行CMake时,它将使用与msvc一起安装的MinGW套件。 我在哪里可以告诉Visual Studio哪个Qt套件是正确的?

感谢@Tsyvarev指出问题并解决。

总结发生了什么:

  • 当Visual Studio运行CMake时,它并不暗示它应该使用MSVC
  • CMake最终找到了系统上安装的MinGW套件

要以(据我所知)持久的方式修复它:

  • 右键CMakeList文件并清除CMake缓存
  • 右键单击CMakeList文件,然后选择“ CMake设置”
  • 然后点击右上角的“编辑JSON”
  • 您可以在JSON中添加CMake变量
{
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "CMAKE_PREFIX_PATH",
          "value": "D:\\Program\\Qt\\5.13.1\\msvc2017_64",
          "type": "STRING"
        }
      ]
    }
  ]
}

请注意,这是定义变量的部分:

    "variables": [
        {
          "name": "CMAKE_PREFIX_PATH",
          "value": "D:\\Program\\Qt\\5.13.1\\msvc2017_64",
          "type": "STRING"
        }
      ]

您应该相应地更新值。 文件的其余部分由Visual Studio生成。

  • 现在生成CMake缓存应使用正确的工具包

再次感谢@Tsyvarev。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM