gpt4 book ai didi

c++ - 如何在没有 QtCreator 的情况下编译 Qt 5 项目?

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:34 25 4
gpt4 key购买 nike

我尝试修改 KDevelop Qt 4 CMake 项目以使用 Qt 5 对其进行编译并更改项目,但我失败了。我还在学习 Qt,这个项目很简单。注释行被替换,使其成为 Qt 5 项目。

主要.cpp:

//#include <QtGui/QApplication>
#include <qt5/QtWidgets/QApplication>
#include "Qt4_Training.h"

int main(int argc, char** argv)
{
QApplication app(argc, argv);
Qt4_Training qt4_training;
qt4_training.show();
return app.exec();
}

Qt4_Training.cpp:

#include "Qt4_Training.h"

/*#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QtGui/QPushButton>
#include <QTime>*/
#include <qt5/QtWidgets/QLabel>
#include <qt5/QtWidgets/QMenu>
#include <qt5/QtWidgets/QMenuBar>
#include <qt5/QtWidgets/QAction>
#include <qt5/QtWidgets/QPushButton>
#include <qt5/QtCore/QTime>

Qt4_Training::Qt4_Training()
{
QLabel *label = new QLabel;
vbox = new QVBoxLayout;

label.setText("Hi.");
vbox->addWidget(label);
QWidget *a = new QWidget;
a->setLayout(vbox);
setCentralWidget(a);

QAction* action = new QAction(this);
action->setText( "Quit");
connect(action, SIGNAL(triggered()), SLOT(close()));
menuBar()->addMenu( "File" )->addAction(action);
}

Qt4_Training::~Qt4_Training() {}

//#include "Qt4_Training.moc"

Qt4_Training.h:

#ifndef Qt4_Training_H
#define Qt4_Training_H

/*#include <QtGui/QMainWindow>
#include <QtGui/QVBoxLayout>*/
#include <qt5/QtWidgets/QMainWindow>
#include <qt5/QtWidgets/QVBoxLayout>

class Qt4_Training : public QMainWindow
{
//Q_OBJECT

public:
QVBoxLayout *vbox;
Qt4_Training();
virtual ~Qt4_Training();
};

#endif // Qt4_Training_H

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)
project(qt4_training)
#find_package(Qt4 REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)

#include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${Qt5Widgets_INCLUDE_DIRS} ${Qt5Widgets_LIBRARIES} ${CMAKE_CURRENT_BINARY_DIR} ${Qt5Core_INCLUDE_DIRS} ${Qt5Core_LIBRARIES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
set(Qt4_Training_SRCS Qt4_Training.cpp main.cpp)

#qt4_automoc(${Qt4_Training_SRCS})
add_executable(qt4_training ${Qt4_Training_SRCS})
target_link_libraries(qt4_training ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})

install(TARGETS qt4_training RUNTIME DESTINATION bin)

我还尝试使用 Eclipse 并使用了“#include”,但所有对象名称都必须以“Private”结尾,例如“QLabelPrivate ...;”。

KDevelop 错误:

...
CMakeFiles/qt4_training.dir/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x37): undefined reference to `QApplication::QApplication(int&, char**, int)'
main.cpp:(.text.startup+0x47): undefined reference to `QWidget::show()'
main.cpp:(.text.startup+0x4c): undefined reference to `QApplication::exec()'
main.cpp:(.text.startup+0x5f): undefined reference to `QApplication::~QApplication()'
main.cpp:(.text.startup+0x96): undefined reference to `QApplication::~QApplication()'
collect2: error: ld returned 1 exit status
CMakeFiles/qt4_training.dir/build.make:110: recipe for target 'qt4_training' failed
make[2]: *** [qt4_training] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/qt4_training.dir/all' failed
make[1]: *** [CMakeFiles/qt4_training.dir/all] Error 2
Makefile:117: recipe for target 'all' failed
make: *** [all] Error 2
*** Failure: Exit code 2 ***

eclipse 错误:

In file included from ../src/Qt 5 Training.cpp:1:0:
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qapplication.h:37:37: fatal error: QtCore/qcoreapplication.h: No such file or directory
#include <QtCore/qcoreapplication.h>
^
compilation terminated.
src/subdir.mk:21: recipe for target 'src/Qt 5 Training.o' failed
make: *** [src/Qt 5 Training.o] Error 1

我在 Kubuntu 上安装了 libqt5widgets5、qtdeclarative5-dev、libqt5gui5、qtbase5-dev...。那么我有 Qt 5 吗?

最佳答案

http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html 查看 cmake-qt 的文档

据此
target_link_libraries(qt4_training Qt5::Widgets)

可能会修复您的链接器错误。

你的 include 目录看起来也不对,你不应该在这里添加 LIBRARIES 变量

关于c++ - 如何在没有 QtCreator 的情况下编译 Qt 5 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29040774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com