how to add zlib to an existing qt installation
zlib is contained in the core Qt libraries. If you want to use the zlib functions in a Qt program, you only have to include zlib.h which is in src/3rdparty/zlib. See e.g. the implementation of QByteArray in src/corelib/tools.
If you want to use quazip, just add the library to your project. It is based on the Qt libraries. Take care to build the correct qyazip library that corresponds to your Qt installation.
You get the correct include path by adding the following line to your project file:
INCLUDEPATH += $$[QT_INSTALL_PREFIX]/src/3rdparty/zlib
For Qt5, see Thorbjørn's comment: it is sufficient to use #include <QtZlib/zlib.h>
.
zlib in Qt - QtZlib not present
Qt's zlib is an internal implementation detail. You're not supposed to use it. You need to link your own copy of zlib, just as you would need to if you weren't using Qt at all.
Build a 3rd party library from source within an existing Qt project
Following are instructions for adding a 3rd party repository to your Qt project and building it from source.
Some are able to add such libraries via Qt Creator, but I could never get that to work. So these are instructions on how to create the necessary .pro
and .pri
files instead. In this post, I will use zlib
as an example, although other libraries should be similar.
Setup Build Order
Since your application depends on this library, we need to ensure that the library is built first. To do this, the 3rd party library and the source code for your application should be in sibling directories.
~/myApp $ ls myApp
src zlib
You probably already have a myApp.pro
file that correctly builds your application. I recommend renaming it to src.pro
, and you'll see why in the next step.
mv src/myApp.pro src/src.pro
Now you can create a new myApp.pro
in the root directory.
~/myApp $ touch myApp.pro
~/myApp $ ls
myApp.pro src zlib
This is a rather simple .pro
file that merely says "build zlib
before myApp
."
# ~/myApp/myApp.pro
TEMPLATE = subdirs
CONFIG += ordered # This tells Qt to compile the following SUBDIRS in order
SUBDIRS = zlib src
Create Library .pro File
Now we need to tell Qt how to build our 3rd party library. We do this by creating a new .pro
file:
# ~/myApp/zlib/zlib.pro
TARGET = z # The name of the output library - can be whatever you want
TEMPLATE = lib # Tells Qt that we are compiling a library so the output will be bundled into a .a or .so file
CONFIG += staticlib # Tells Qt that we want a static library, so a .a file. Remove this and you will get a .so file
QMAKE_CFLAGS_WARN_ON -= -Wall # Optional - disable warnings when compiling this library
QMAKE_CXXFLAGS_WARN_ON -= -Wall # Optional - disable warnings when compiling this library
HEADERS += \
crc32.h \
deflate.h \
gzguts.h \
inffast.h \
inffixed.h \
inflate.h \
inftrees.h \
trees.h \
zconf.h \
zlib.h \
zutil.h
SOURCES += \
adler32.c \
compress.c \
crc32.c \
deflate.c \
gzclose.c \
gzlib.c \
gzread.c \
gzwrite.c \
infback.c \
inffast.c \
inflate.c \
inftrees.c \
trees.c \
uncompr.c \
zutil.c
If you are building something other than zlib
, just change TARGET
to the name of the library, and replace the contents of HEADERS
and SOURCES
with the files that need to be compiled for your library.
You can go ahead now and test out this .pro
file.
~/myApp/zlib/ $ qmake
~/myApp/zlib/ $ make
...
~/myApp/zlib/ $ ls libz.a
libz.a
Yay!
Link The Library Into Your Application
Finally, we need to update the .pro
file of your application to link in the 3rd party library. There are two parts to this:
- Add library header files to your include path so the compiler can get the defined symbols from there.
- Link the static (or shared) library during compile and linking time so the applicable object code can get linked into your application.
First, we'll add the header files into the include path. Add this line to src.pro
.
INCLUDEPATH += zlib
This allows you to reference zlib.h
in your code like so:
#include "zlib.h"
Otherwise, you would have to specify the full relative path of the header like this:
#include "zlib/zlib.h"
If you're fine with the second method, then there should be no need to update the INCLUDEPATH
variable.
Second, we need to add the arguments the linker needs to find the static (or shared) library itself. Add this line to src.pro
.
LIBS += -L$$PWD/../zlib -lz
The first part (-L$$PWD/../zlib
) says that the folder containing the library is at ../zlib
, which should be correct since zlib
is a sibling folder to src
. The second part (-lz
) says that the name of the library is z
. The linker infers from this that the library is actually located in the file libz.a
.
Done
At this point, you may have to clean your build by doing a make distclean
. But from there you should be able to build your project with the 3rd party library included by going to your base directory and running qmake
and make
.
cd ~/myApp
qmake -r
make
Note: Big thanks go to @LaszloPapp for getting this process started. Much of the source material for this answer came from his answer.
How can zlib.cpp in boost ever compile?
You're supposed to have the library dependencies installed.
This means that zlib.h
is in your system's include directories.
Your IDE might silently add some convenient libraries your include paths.
Build c++ Qt CLI Tool with Quazip
From QuaZip's pro file:
# You'll need to define this one manually if using a build system other
# than qmake or using QuaZIP sources directly in your project.
CONFIG(staticlib): DEFINES += QUAZIP_STATIC
This should trigger when you add staticlib
to the CONFIG
in quazip.pro (2nd line):
CONFIG += qt warn_on staticlib
If you are not using qmake to build Quazip, just make sure that you #define QUAZIP_STATIC
in a way specific to your build system
Related Topics
C++ Multi-Line Comments Using Backslash
How Serious Is the New/Delete Operator Mismatch Error
What Are the Advantages of Using the C++ Boost Libraries
How to Set Up Unit Testing for Visual Studio C++
How to Compile C++ with C++11 Support in MAC Terminal
What Does Static_Assert Do, and What Would You Use It For
Memory Alignment:How to Use Alignof/Alignas
Should One Never Use Static Inline Function
Difference Between Std::Function<> and a Standard Function Pointer
String::Size_Type Instead of Int
Passing Object by Reference in C++
Svm Classifier Based on Hog Features for "Object Detection" in Opencv
Usage of Std::Forward VS Std::Move
Disabling Warnings Generated via _Crt_Secure_No_Deprecate
Std::Remove_If - Lambda, Not Removing Anything from the Collection
Do Function Pointers Need an Ampersand
How to Generate a Newline in a Cpp MACro
Ways to Detect Whether a C++ Virtual Function Has Been Redefined in a Derived Class