可以使用CMAKE构建QT-Widgets应用程序
我正在尝试设置一个构建QT-Widgets应用程序的CMAKE项目,但无法正确编译。我的项目结构如下:
- 包括/
- mainwindow.hpp
- 资源/
- mainwindow.ui
- src/
- main.cpp
- mainwindow.cpp
- cmakelists.txt
cmakelists.txt
cmake_minimum_required(VERSION 3.10)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
project(Test VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS resources)
add_executable(App src/mainwindow.cpp src/main.cpp resources/mainwindow.ui)
target_include_directories(App PRIVATE include)
target_link_libraries(App PRIVATE Qt5::Widgets)
mainwindow.hpp
#ifndef MAINWINDOW_HPP_
#define MAINWINDOW_HPP_
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class Test : public QMainWindow {
Q_OBJECT
public:
Test(QWidget *parent = nullptr);
~Test();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_HPP_
mainwindow.cpp
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
Test::Test(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}
Test::~Test(){
delete ui;
}
main.cpp
#include "mainwindow.hpp"
#include <QApplication>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
Test w;
w.show();
return a.exec();
}
当我尝试构建项目时:
cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug
cmake --build build --target all
我会收到以下链接器错误:
[ 20%] Automatic MOC and UIC for target App
[ 20%] Built target App_autogen
[ 40%] Building CXX object CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o
[ 60%] Building CXX object CMakeFiles/App.dir/src/mainwindow.cpp.o
[ 80%] Building CXX object CMakeFiles/App.dir/src/main.cpp.o
[100%] Linking CXX executable App
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::Test(QWidget*)': .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::~Test()': .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/App.dir/build.make:132: App] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/App.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
查看CMAKES生成的,似乎正确链接了所需的对象文件,
/usr/bin/c++ \
-g CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o \
CMakeFiles/App.dir/src/mainwindow.cpp.o \
CMakeFiles/App.dir/src/main.cpp.o \
-o App \
/usr/lib/libQt5Widgets.so.5.15.4 \
/usr/lib/libQt5Gui.so.5.15.4 \
/usr/lib/libQt5Core.so.5.15.4
我不确定,为什么VTable引用不确定。我在这里做错了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是,将
添加到源中添加/mainwindow.hpp
。这似乎是生成的
ui_mainwindow.h
看到Mainwindow类的VTable条目。The solution to this is, adding
include/mainwindow.hpp
to the sources.This seems to be required for the generated
ui_mainwindow.h
to see the vtable entries of the MainWindow class.