Adding an Internal Library to a qmake Project

You can add a library into a subdirs project. Use wizards to create the project and the library and to link the library against the project.

Note: This tutorial only applies when you select qmake as the build system for the subdirs project.

Creating a shared library

To create a shared library:

  1. Select File > New Project > Library > C++ Library. If your top level project is a subdirs project or contains one, you may add the library to the project. However, this does not link other libraries from your project against it.
  2. Select Choose to open the Project Location dialog.

    {Project Location dialog}

  3. In the Name field, give a name for the library. For example, MyLibrary.
  4. Select Next (on Windows and Linux) or Continue (on macOS) to open the Define Build System dialog.
  5. Select Next or Continue to use CMake as the build system.

    The Define Project Details dialog opens.

    {Define Project Details dialog}

  6. Select the library type and enter information about the classes for which you want to generate source code files: class name, Qt module, and source and header file names.
  7. Select Next or Continue until you get to the Project Management dialog. In the Add as a subproject to project list, select a subdirs project. For example, MyApplication.

Linking an application to the library

To link a project to the shared library:

  1. In the Projects view, right-click the project name to open the context menu and select Add Library > Internal Library > Next.

    The wizard instructs the build system to link an existing application project or a library project against the selected library. Here, you add the library that you created above.

    {Adding an internal library}

  2. In the Library field, select mylibrary, and then select Next.
  3. Select Finish to add the library declaration to the project file.

When using CMake, the target_link_libraries command is added to the CMakeLists.txt file:

target_link_libraries(myapplication PRIVATE mylibrary)

When using qmake, the following library declaration is added to the .pro file:

win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../../projects/mylib/release/ -lmylib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../../projects/mylib/debug/ -lmylib
else:unix: LIBS += -L$$OUT_PWD/../../../projects/mylib/ -lmylib
INCLUDEPATH += $$PWD/../../../projects/mylib
DEPENDPATH += $$PWD/../../../projects/mylib
win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../projects/mylib/release/mylib.lib
else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../projects/mylib/debug/mylib.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../../projects/mylib/libmylib.a

See also Add libraries to qmake projects, Add libraries to CMake projects, Add subprojects to projects, Select the build system, Use project wizards, and Creating Projects.

© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.