Tutorial: Use internal libraries with qmake | Qt Creator Documentation
Description: How to create your own library and link your application against it when using qmake as the build system.

- Qt Creator Documentation
- Tutorial: Use internal libraries with qmake
- Qt Creator 15.0.0

Tutorials ◦ How To

# Tutorial: Use internal libraries with qmake

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.

## Create a shared library

To create a shared library:

1. Go to File > New Project, and select 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.

3. In Name, 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. In Build system, select qmake.
6. Select Next or Continue to open the Define Project Details dialog.

7. 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.
8. 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.

## Link 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.

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

The wizard adds the following library declaration 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.

Tutorials ◦ How To