我正在尝试使用 cmake 构建一个 qt 项目。首先,我首先在qt creator中编写了一个简单的代码,其中包含main.cpp,mainwindow.cpp和mainwindow.h。下面是我的CMakeLists.txt配置。
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确实成功地生成了VS项目文件。但是,当我尝试在Visual Studio中构建它们时,它给了我链接错误。下面列出了一些错误。我无法发布整个错误列表,因为它太长了。
>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
我正在使用Qt5.3.1,cmake 3.0.2和Visual Studio 2012。
为了解决此问题,您必须从中删除 WIN32 语句 add_executable(testproject WIN32 main.cpp mainwindow.cpp)
.
可能的原因:
- 检查
CMakeLists.txt
内add_executable
命令中的源文件名是否存在拼写错误。源列表中不存在的文件名将生成链接器错误error LNK2019: unresolved external symbol main referenced
。 - 您的入口点函数是什么样的?
-
WinMain
需要add_executable(testproject WIN32 main.cpp mainwindow.cpp)
-
main
需要add_executable(testproject main.cpp mainwindow.cpp)
- 注意:我建议使用
int main(int argc, char *argv[])
,因为它可以移植到 linux,而使用WinMain
则不会。
-
我刚刚遇到了这个错误,我在Visual Studio中使用了x64-Debug
配置,但是CMake中的变量CMAKE_PREFIX_PATH
是x86 Qt的路径,因此链接器尝试将x86 Qt库链接到x64程序并发生了错误。
解决方案是将Visual Studio配置和Qt路径更正为同一体系结构。
链接到QtCore
,你只能链接到Qt5::Widgets
。将Qt5::Core
添加到链接库。至少对我来说,CMake 文件不包含传递依赖项(因此针对 Qt5::Widgets
的链接是不够的。