qt_add_library

Creates and finalizes a library.

The command is defined in the Core component of the Qt6 package, which can be loaded like so:

 find_package(Qt6 REQUIRED COMPONENTS Core)

This command was introduced in Qt 6.2.

Synopsis

 qt_add_library(target
     [STATIC | SHARED | MODULE | INTERFACE | OBJECT]
     [MANUAL_FINALIZATION]
     sources...
 )

If versionless commands are disabled, use qt6_add_library() instead. It supports the same set of arguments as this command.

Description

qt_add_library() is a wrapper around CMake's built-in add_library() command. It performs the following tasks:

  • Create a CMake target of an appropriate library type.
  • Handle finalization of the CMake target.

Target Creation

The type of library created can be specified explicitly with one of the STATIC, SHARED, MODULE, INTERFACE or OBJECT keywords, just as one might for add_library(). If none of these keywords are given, the library type created depends on how Qt was built. If Qt was built statically, a static library will be created. Otherwise, a shared library will be created. Note that this is different to how CMake's add_library() command works, where the BUILD_SHARED_LIBS variable controls the type of library created. Since 6.7, the qt_add_library() command considers BUILD_SHARED_LIBS when deciding the library type only if the variable is set explicitly and QTP0003 is set to NEW.

Any sources provided will be passed through to the internal call to add_library().

Finalization

After a target is created, further processing or finalization steps may be needed. The finalization processing is implemented by the qt_finalize_target() command.

Finalization can occur either as part of this call or be deferred to sometime after this command returns (but it should still be in the same directory scope). When using CMake 3.19 or later, finalization is automatically deferred to the end of the current directory scope. This gives the caller an opportunity to modify properties of the created target before it is finalized. When using CMake versions earlier than 3.19, automatic deferral isn't supported. In that case, finalization is performed immediately before this command returns.

Regardless of the CMake version, the MANUAL_FINALIZATION keyword can be given to indicate that you will explicitly call qt_finalize_target() yourself instead at some later time. In general, MANUAL_FINALIZATION should not be needed unless the project has to support CMake 3.18 or earlier.

See also qt_finalize_target(), qt_add_executable(), and QTP0003.