Linking Error using Cmake file to build Qt project in Visual Studio
-
Hi all,
I am trying to build a qt project using cmake. As a startinng point, I started off by just writing a simple code in qt creator, which contains main.cpp, mainwindow.cpp & mainwindow.h. Below are my CMakeLists.txt configuration.
@
cmake_minimum_required(VERSION 2.8.11)project(testproject)
Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
Find the QtWidgets library
find_package(Qt5Widgets)
find_package(Qt5Core)get_target_property(QtCore_location Qt5::Core LOCATION)
Tell CMake to create the executable
add_executable(testproject WIN32 main.cpp mainwindow.cpp)
Use the Widgets module from Qt 5.
target_link_libraries(testproject Qt5::Widgets)
@Cmake did successfully generate the build files. However, when I try to build the executable in visual studio, it gives me linking errors. Some of the errors are listed below. I could not post the whole error list as it is too long.
@>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QWidget::show(void)" (_imp?show@QWidget@@QEAAXXZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QApplication::QApplication(int &,char * *,int)" (_imp??0QApplication@@QEAA@AEAHPEAPEADH@Z) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QApplication::~QApplication(void)" (_imp??1QApplication@@UEAA@XZ) referenced in function main
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (_imp?exec@QApplication@@SAHXZ) referenced in function main
2>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QMetaObject::Connection::~Connection(void)" (_imp??1Connection@QMetaObject@@QEAA@XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
2>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QString::~QString(void)" (_imp??1QString@@QEAA@XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
2>testproject_automoc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual int __cdecl QMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (_imp?qt_metacall@QMainWindow@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) referenced in function "public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MainWindow@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
2>testproject_automoc.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (_imp?staticMetaObject@QMainWindow@@2UQMetaObject@@B) referenced in function "void __cdecl `dynamic initializer for 'public: static struct QMetaObject const MainWindow::staticMetaObject''(void)" (??__E?staticMetaObject@MainWindow@@2UQMetaObject@@B@@YAXXZ)
2>MSVCRT.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain referenced in function __tmainCRTStartup@I am using Qt5.3.1, cmake 3.0.2 and visual studio 2012.
How can I solve this problem? Any advice/help is really appreciated.
Thank you in advance.