Reputation: 3202
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.
Upvotes: 3
Views: 4988
Reputation: 10857
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
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.
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:
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)
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").
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...
Upvotes: 4