Add libraries to CMake projects

In addition to Qt libraries, you can add other libraries to your projects. The process depends on the type and location of the library:

  • A system library
  • Your own library
  • A 3rd party library

Qt Creator supports code completion and syntax highlighting for the added libraries once your project successfully builds and links to them.

Add your own libraries

Use the qt_add_library command to create a library and link against it in the CMakeLists.txt file, as instructed in Structuring projects.

Specify whether the library is statically or dynamically linked. For a statically linked internal library, add the CMake: target_link_libraries command to the CMakeLists.txt project file to specify dependencies.

Add external libraries

Through external libraries, Qt Creator can support code completion and syntax highlighting as if the code were a part of the current project or the Qt library.

Qt Creator detects the external libraries using the CMake: find_package command command. Some libraries come with the CMake installation. You can find those in the Modules directory of your CMake installation. For more information, see CMake: cmake-packages(7).

Use local CMake Find packages

For CMake projects that have external dependencies, use Find<package>.cmake modules that expose imported targets. You can use the pre-defined sample_find_module code snippet to add sample commands to a .cmake file. You can then change the commands as necessary.

Place Find modules in the ${CMAKE_CURRENT_SOURCE_DIR}/cmake directory, and append the directory name to the CMAKE_MODULE_PATH list variable. For example:

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

See also How To: Build with CMake, CMake, Completion, and Snippets.

© 2024 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.