简体   繁体   English

带有CLion的Windows中使用CMake(mingw)在C ++中未定义的Qt5引用

[英]Undefined Qt5 references in C++ using CMake (mingw) in Windows with CLion

I am trying to learn Qt5 with CMake in the new CLion C/C++ IDE , I am relatively new with these tools. 我正在尝试在新的CLion C / C ++ IDE中 使用CMake学习Qt5 ,这些工具对我来说还是比较新的。 So, I have this real simple code main.cpp : 所以,我有这个真正的简单代码main.cpp

#include <QDebug>
int main() {
    qDebug("Hello World!");
    return 0;
}

I've followed the documentation and ended with this CMakeLists.txt : 我遵循了文档并以CMakeLists.txt结尾:

cmake_minimum_required(VERSION 2.8.11)
project(cl_hello_world2)

set(CMAKE_PREFIX_PATH D:/Qt/Qt5.4.0/5.4/msvc2012_opengl)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_VERBOSE_MAKEFILE ON)

find_package(Qt5Widgets)

set(SOURCE_FILES main.cpp)
add_executable(cl_hello_world2 WIN32 ${SOURCE_FILES})

target_link_libraries(cl_hello_world2 Qt5::Widgets)

After hours and hours of trying, I keep having these undefined reference errors: 经过数小时的尝试,我仍然遇到这些undefined reference错误:

CMakeFiles\cl_hello_world2.dir/objects.a(main.cpp.obj): In function `main':
E:/Qt/test/cl-hello-world2/main.cpp:4: undefined reference to `_imp___ZNK14QMessageLogger5debugEPKcz'

This is the compiler command that CMake is executing: 这是CMake正在执行的编译器命令:

D:\PROGRA~2\CODEBL~1\MinGW\bin\G__~1.EXE   -std=c++11 -g    -mwindows -Wl,--whole-archive CMakeFiles\cl_hello_world2.dir/objects.a -Wl,--no-whole-archive  -o cl_hello_world2.exe -Wl,--out-implib,libcl_hello_world2.dll.a -Wl,--major-image-version,0,--minor-image-version,0  D:\Qt\Qt5.4.0\5.4\msvc2012_opengl\lib\Qt5Widgetsd.lib D:\Qt\Qt5.4.0\5.4\msvc2012_opengl\lib\Qt5Guid.lib D:\Qt\Qt5.4.0\5.4\msvc2012_opengl\lib\Qt5Cored.lib D:\Qt\Qt5.4.0\5.4\msvc2012_opengl\lib\qtmaind.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

I don't know if this line is syntactically correct, but the library D:\\Qt\\Qt5.4.0\\5.4\\msvc2012_opengl\\lib\\Qt5Widgetsd.lib exists... 我不知道这行在语法上是否正确,但是库D:\\Qt\\Qt5.4.0\\5.4\\msvc2012_opengl\\lib\\Qt5Widgetsd.lib存在...

I've try with Qt5.3.2 and Qt 5.4.0. 我尝试使用Qt5.3.2和Qt 5.4.0。 Maybe you know some other way to add the library for the linking or maybe I've made a mistake... 也许您知道添加链接库的其他方法,或者我犯了一个错误...

Thanks. 谢谢。

Your problem is you are attempting to use a Visual Studio build of Qt with mingw. 您的问题是,您正在尝试使用mingw使用Qt的Visual Studio版本。 You need to use a mingw build of Qt. 您需要使用mingw版本的Qt。

Edit 编辑

  1. If you are going to use MinGW, download the right Qt Version at http://www.qt.io/download-open-source/ Qt 5.4.0 for Windows 32-bit (MinGW 4.9.1, 852 MB) . 如果要使用MinGW,请在http://www.qt.io/download-open-source/ Qt 5.4.0(适用于Windows 32位)上下载正确的Qt版本(MinGW 4.9.1,852 MB) At the moment of installation don't forget to select MinGW 4.9.1 component. 在安装时,不要忘记选择MinGW 4.9.1组件。

  2. Follow this step if you are going to use CLion: In File : Settings or Ctrl + Alt + S, choose the correct path of MinGW as shown: 如果要使用CLion,请执行以下步骤:在File:Settings或Ctrl + Alt + S中,选择正确的MinGW路径,如下所示:

CLion工具链配置

  1. In CMakeLists.txt change the line: set(CMAKE_PREFIX_PATH D:/Qt/Qt5.4.0/5.4/msvc2012_opengl) for where your Qt path is: set(CMAKE_PREFIX_PATH D:/Qt/Qt5.4.0MinGw/5.4/mingw491_32) CMakeLists.txt中,更改以下行: set(CMAKE_PREFIX_PATH D:/Qt/Qt5.4.0/5.4/msvc2012_opengl) ,其中Qt路径位于: set(CMAKE_PREFIX_PATH D:/Qt/Qt5.4.0MinGw/5.4/mingw491_32)

  2. Now if you compile and run from the IDE you will get the error: Process finished with exit code -1073741515 (0xC0000135) . 现在,如果您从IDE编译并运行,您将收到错误: Process finished with exit code -1073741515 (0xC0000135) You need the Qt libraries in your path or in the directory where your exe is. 您需要在路径或exe所在目录中的Qt库。 I had to copy icudt53.dll icuin53.dll icuuc53.dll libgcc_s_dw2-1.dll libstdc++-6.dll libwinpthread-1.dll Qt5Cored.dll Qt5Guid.dll Qt5Widgetsd.dll in my directory (the "d" is for "debug"). 我必须将icudt53.dll icuin53.dll icuuc53.dll libgcc_s_dw2-1.dll libstdc++-6.dll libwinpthread-1.dll Qt5Cored.dll Qt5Guid.dll Qt5Widgetsd.dll到我的目录中(“ d”表示“调试” )。

  3. If you run this sample code for Qt beginners from CLion you will get this! 如果您从CLion 为Qt初学者运行此示例代码,您将得到它!

从CLion运行Qt MinGW和CMake

About my initial code I do not understand what is happening but I can't figure it out why qDebug doesn't print in stdout... 关于我的初始代码,我不知道发生了什么,但是我无法弄清楚为什么qDebug无法在stdout中打印...

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

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