Qt compiles using the Quazip library

1. Compile zlib

The quazip library is a Qt-based compression and decompression library that encapsulates the zlib library. Therefore, to use the quazip library, you need to compile zlib first.

Download zlib .

In order to be used in Qt, the compilation environment uniformly uses Qt's MinGW tool chain.
Unzip the source code and open Qt's MinGW command line console.

make -f ./win32/Makefile.gcc

insert image description here

Copy out zconf.h, zlib.h, and zlib1.dllspare:

insert image description here

2. Compile quazip

It took half a day to get it done here. The online tutorials are all old versions, using .prothe file, qmake compiles. The latest version does not use qmake to compile. Because I made an error in running the command earlier, the content of my CMakeLists.txt file was replaced, and then no compilation was successful. After the file content is replaced, the entire project is still compiled, but the executable file is compiled. However, due to the lack of some environment settings, Qt and related libraries cannot be found when linking. Later, the source code file was decompressed again before the compilation passed.

Download the source code .

There are compilation tutorials on the homepage. It's really simple, just two commands and I wasted a lot of time by mistake. Be sure to use Qt's MinGW toolchain, do not use msys2 or the compilation toolchain installed by yourself, otherwise it will fail to compile. In addition, it is best to install the windows version of cmake instead of using msys2.

Unzip the source code and enter the source code directory. Then follow the tutorial on the gateway homepage to perform localized modification and execution.

mkdir build; cd build
cmake -S ../ -B ./ -D QUAZIP_QT_MAJOR_VERSION=5 -G "MinGW Makefiles"

insert image description here

The command execution reported an error. cmake will find the zlib library. However, since zlib is not in the search path of cmake, it cannot find it and reports an error. Since the above zlib is not compiled using cmake, here, modify the CMakeLists.txt file in quazip to shield this problem and add the zlib search path.

Comment out the commands related to searching for zlib in the source directory CMakeLists.txt:

insert image description here

Then modify quazipthe CMakeLists.txt in the directory to directly add the zlib library:

link_directories("D:\\library\\lib_zlib\\lib")

add_library(${
    
    QUAZIP_LIB_TARGET_NAME} ${
    
    QUAZIP_SOURCES})
add_library(QuaZip::QuaZip ALIAS ${
    
    QUAZIP_LIB_TARGET_NAME})

target_link_libraries(${
    
    QUAZIP_LIB_TARGET_NAME} zlib1.dll)

Generated again, still reporting an error. Bzip2 can not be downloaded.

insert image description here

Try again, still can't download, but no error is reported.

insert image description here

Since there is nothing wrong, ignore it and compile directly:

cmake --build . -j20

insert image description here

The dynamic library was generated successfully. Although there are problems ahead. For the previous problem that bzip2 could not be downloaded, you can download it manually and then compile it. I don't care here. Here will be the dynamic library and header files

Copy out libquazip1-qt5.dlland all header files for later use.

insert image description here

3. Add to Qt project

Add relevant code in our project.

#include "mainwindow.h"
#include <QApplication>
#include <libquazip/include/JlCompress.h>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    JlCompress::compressDir("D:\\library\\zlib.zip", "D:\\library\\lib_zlib");
    return a.exec();
}

Add the header file path and dynamic library in .prothe file, add the path of the library file we generated earlier, here only need to add the quazip library.

LIBS += -L$$PWD/libquazip/ -lquazip1-qt5
INCLUDEPATH += $$PWD/libquazip

Then compile, there is no problem. But when running, it will hang up directly because the dynamic library cannot be found. Because I only support dynamic linking here, I need to put the two dynamic library files generated by the previous compilation into the running directory. The Qt Creator debug run directory is similar build-Onekey_Pi-Desktop_Qt_5_12_12_MinGW_32_bit-Debug.

Put zlib1.dlland libquazip1-qt5.dlltwo dynamic libraries in this directory, and then debug and run. The program can start normally, otherwise, the lack of one will directly cause the program to start and exit.

After running, the compression is successful:

insert image description here

Guess you like

Origin blog.csdn.net/duapple/article/details/131117426