written in front
This article is to configure the third-party library yaml in Qt create. The compiler uses MinGW32 (not vs)
1. System environment
Here I only describe the environment I used when configuring
- win11 64 bit
- Qt 5.14.2
- yaml-cpp 0.7.0
- CMake 3.23.2
- Compiler: MinGW32
(yaml-cpp-master seems to have some problems. It will report an error every time it is compiled. It is recommended to use 0.7.0)
Regarding configuring MinGW32, please see my other article, point 2: Configuring environment variables
2. Download yaml-cpp 0.7.0
Download yaml-cpp from Github
and unzip it, open the unzipped directory, and create a new build
folder named inside.
3、CMake
Open camke-gui
①Select directory:
My path here is:
# Where is the source code:
H:/google_download/yaml-cpp-yaml-cpp-0.7.0
# Where to build the binaries:
H:/google_download/yaml-cpp-yaml-cpp-0.7.0/build
! ! ! Notice! ! !
The following paths were originally written based on yaml-cpp-master. Please change it to yaml-cpp-yaml-cpp-0.7.0 now.
②Configure option
Click configure
then select
Compilers
# C
D:/software/Qt/Qt5.14.2/Tools/mingw730_64/bin/gcc.exe
# C++
D:/software/Qt/Qt5.14.2/Tools/mingw730_64/bin/g++.exe
Click Finish and don't worry if it
appears in red. When you see it, Configuring done
click Generate to turn off cmake-gui.
Generating done
4. Compile
build
Enter the folder we just created , and you will see the content output by CMake just now.
Place the mouse in the folder interface, hold down the shift key, and right-click the mouse at the same time. Click to open the Powershell window(s) here, enter Windows Powershell and enter the following
command, and press Enter to execute the command.
mingw32-make -j 8
The purpose here -j 8
is to allow the CPU to perform multi-tasking and speed up compilation. Make settings according to your computer's configuration.
Wait for the terminal to finish running. . . . . . . . .
You can build
see it in the folder libyaml-cpp.a
. This is the file we need to use later.
There is another folder that needs to be used, which is in the yaml-cpp-master directory.include
5. Test with Qt
①Create a new Qt project
Create a new project in Qt Create and follow the normal process. When selecting kits, be sure to select MinGw 64.
②Move xxx.a file
lib
Create a new folder in the Qt project directory and put
what we just generated libyaml-cpp.a
into it.
③Add yaml-cpp
Add the following content to xxx.pro
the file
and modify it according to your actual situation: INCLUDEPATH is the folder
we just mentioned , which is in the yaml-cpp-master directory.include
include
INCLUDEPATH += H:\google_download\yaml-cpp-master\include
LIBS += $$PWD/lib/libyaml-cpp.a
④Execute qmake
⑤Test
First prepare a yaml file, test.yaml
the content is:
name: Hunzi
num: 123
Import the header file in mainwindow.cpp
#include "yaml-cpp/yaml.h"
using namespace YAML;
In the constructor write:
Node config;
try{
config = LoadFile("H:/TempFiles/test.yaml");
} catch(BadFile &e) {
qDebug() <<"read error!";
}
qDebug() << "name:" << QString::fromStdString(config["name"].as<std::string>()) << endl;
qDebug() << "num:" << config["num"].as<int>() << endl;
⑥Check the kit
⑦Run
question
If the kit is not chosen by me MingGW 64
, for example, if I choose it here, MSVC2017 64
you only MingGW 64
need to change it back.
At this point, Qt5.14.2 configuration yaml-cpp is completed.