How do I create static and shared libraries in qmake for C++?

Creating static and shared libraries in C++ using qmake is a straightforward process. Qmake is a tool that helps in managing the build process of software projects across different platforms, and it makes the creation of libraries very easy. This guide will help you understand how to create both static and shared libraries using qmake.

Creating a Static Library

To create a static library, you need to specify the library type in your .pro file. Below is an example:

TEMPLATE = lib CONFIG += static LIBS = -L$$PWD -lname_of_library INCLUDEPATH += $$PWD/include SOURCES += src/file1.cpp \ src/file2.cpp HEADERS += include/file1.h \ include/file2.h

Creating a Shared Library

For creating a shared library, you need to change the configuration in your .pro file as follows:

TEMPLATE = lib CONFIG += shared LIBS = -L$$PWD -lname_of_library INCLUDEPATH += $$PWD/include SOURCES += src/file1.cpp \ src/file2.cpp HEADERS += include/file1.h \ include/file2.h

Building the Libraries

After you have set up your .pro file, you can build the libraries by running the following commands in your terminal:

qmake name_of_library.pro make

This will generate either a static library (.a) or a shared library (.so), depending on the configuration specified in your .pro file.


C++ qmake static library shared library build process