简体   繁体   中英

Linking Error using Cmake file to build Qt project in Visual Studio

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 VS Project files. However, when I try to build them 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.

为了解决这个问题,你必须从add_executable(testproject WIN32 main.cpp mainwindow.cpp)删除 WIN32 语句。

You have to link against QtCore , you only link against Qt5::Widgets . Add Qt5::Core to your link libraries. At least for me, the CMake files don't include transitive dependencies (so linking against Qt5::Widgets is not enough.)

Possible causes:

  • Check the source file names in the add_executable command inside your CMakeLists.txt for spelling errors. File names in the source list that do not exist will produce the linker error error LNK2019: unresolved external symbol main referenced .
  • What does your entry point function look like?
    • WinMain requires add_executable(testproject WIN32 main.cpp mainwindow.cpp)
    • main requires add_executable(testproject main.cpp mainwindow.cpp)
    • Note : I suggest using int main(int argc, char *argv[]) , as it will be portable to linux, while using WinMain won't.

I just ran into this error, I was using the x64-Debug configuration in Visual Studio , but the variable CMAKE_PREFIX_PATH in CMake was the path of x86 Qt , so the linker tried to link the x86 Qt libraries to the x64 program and the error occurred.

The solution is correcting the Visual Studio configuration and the Qt path to the same architecture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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