通过CMake在没有Qt Creator的情况下构建Qt5 Quick项目 [英] Building Qt5 Quick project without Qt Creator via CMake
问题描述
我正在尝试构建简单的Qt Quick项目,并希望在 Qt Creator
之外使用它。这意味着只能使用 Vim
或 CMake
之类的工具进行编码,构建和运行程序。
Im trying to build simple Qt Quick project and want to work with it outside Qt Creator
. It means coding, building and running program only with tools like Vim
or CMake
.
我有两个问题:
- 我不明白为什么CMake本身无法找到所有需要的库并进行构建
- 我不明白为什么在Qt Creator中单击
▶
按钮会成功构建默认项目,但是我自己运行CMake的结果
- I don't understand why CMake cannot find all needed libraries by itself and build project.
- I don't understand why clicking on
▶
button in Qt Creator builds default project succesfully, but running CMake by myself results in c++ error.
首先,我在github上找到的Qt5 Quick项目中制作了CMakeLists.txt。然后,我尝试通过Creator创建项目,然后检查它如何构建 CMakeLists.txt
并由我自己编写一个项目。
Firstly i made CMakeLists.txt in the image of Qt5 Quick project i found on github. Then I tried to make project via Creator, then inspect how it builds CMakeLists.txt
and compose one by myself.
我的第一次尝试:
cmake_minimum_required(VERSION 3.11..3.15)
set(PROJECT_NAME "uint32_sort_gui")
project(${PROJECT_NAME} LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5 COMPONENTS Core Gui Qml Quick QuickControls2 REQUIRED)
add_subdirectory(${PROJECT_SOURCE_DIR}/sort_lib/)
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Qml
Qt5::Quick
Qt5::QuickControls2 #(*)
SortCore
)
set_target_properties(${PROJECT_NAME}
PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON)
第二个(由Qt Creator自动生成):
Second (autogenerated by Qt Creator):
cmake_minimum_required(VERSION 3.1)
project(Deleteme2 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
在(*)
行上,第一个列表失败,说
First listing fails on (*)
line, saying
Could not find a package configuration file provided by "Qt5QuickControls2"
with any of the following names:
Qt5QuickControls2Config.cmake
qt5quickcontrols2-config.cmake
Add the installation prefix of "Qt5QuickControls2" to CMAKE_PREFIX_PATH or
set "Qt5QuickControls2_DIR" to a directory containing one of the above
files. If "Qt5QuickControls2" provides a separate development package or
SDK, be sure it has been installed.
(但是我已经安装了所有东西)
(But i've installed everything)
第二个失败,并在编译期间出错:
And second fails with error during compilation:
make
[ 16%] Automatic MOC for target Deleteme2
[ 16%] Built target Deleteme2_autogen
[ 33%] Building CXX object CMakeFiles/Deleteme2.dir/main.cpp.o
Deleteme2/main.cpp: In function ‘int main(int, char**)’:
Deleteme2/main.cpp:6:36: error: ‘AA_EnableHighDpiScaling’ is not a member of ‘Qt’
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
我在做什么错?为什么Qt Creator可以毫无问题地构建 Deleteme2
?
What am i doing wrong? Why Qt Creator can build Deleteme2
without a problem?
编辑:
这是示例代码,由Qt Creator生成的第二次尝试
Edited: Here is the sample code, generated by Qt Creator for second attempt
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
推荐答案
主要问题,为什么 第一次尝试失败是因为Qt5将自己安装在
〜/ Qt
中,而CMake则搜索了 *。cmake
/ usr / lib / x86_64-linux-gnu / cmake /
中的配置文件(我没想到我使用的是 Ubuntu 16 LTS
,其中已预先安装了cmake 3.5.1)。
The main problem, why "First attempt" failed is because Qt5 installed itself in
~/Qt
and CMake searched for *.cmake
configuration files in /usr/lib/x86_64-linux-gnu/cmake/
(I didn't metion that i use Ubuntu 16 LTS
, which have cmake 3.5.1 preinstalled).
所以解决此问题的方法是复制
So the way to solve this problem was to copy everything that was in
〜/ Qt / *最新cmake版本* / gcc_64
(在我的情况下为〜/ Qt / 5.13.0 / gcc_64
)
通过 sudo cp -a〜/ Qt / 5.13.0 / gcc_64 /进入
/ usr / lib /
* / usr / lib /
into /usr/lib/
via sudo cp -a ~/Qt/5.13.0/gcc_64/* /usr/lib/
秒的答案很短:
发生这种情况是因为默认情况下,Qt Creator将CMake输出设置为Ninja makefile。
This happend because by default Qt Creator sets CMake output into Ninja makefiles.
转到工具> ;选项>工具包
并添加所需的配置或编辑现有配置。
Go to Tools > Options > Kits
and add desired configuration or edit existing.
这篇关于通过CMake在没有Qt Creator的情况下构建Qt5 Quick项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!