在C ++中使用CMake(mingw)在CLion中使用未定义的Qt5引用 [英] Undefined Qt5 references in C++ using CMake (mingw) in Windows with CLion

查看:4866
本文介绍了在C ++中使用CMake(mingw)在CLion中使用未定义的Qt5引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在新的 将从这里获得!




关于我的初始代码我不明白发生了什么,但我不知道为什么qDebug不打印在stdout ...


I am trying to learn Qt5 with CMake in the new CLion C/C++ IDE, I am relatively new with these tools. So, I have this real simple code main.cpp:

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

I've followed the documentation and ended with this 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:

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:

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...

I've try with Qt5.3.2 and 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. You need to use a mingw build of 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). At the moment of installation don't forget to select MinGW 4.9.1 component.

  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:

  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)

  2. Now if you compile and run from the IDE you will get the error: Process finished with exit code -1073741515 (0xC0000135). You need the Qt libraries in your path or in the directory where your exe is. 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").

  3. If you run this sample code for Qt beginners from CLion you will get this!

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...

这篇关于在C ++中使用CMake(mingw)在CLion中使用未定义的Qt5引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆