On this page

Add libraries to CMake projects

With CMake, you can:

  • Import existing executable or library files from disk into a CMake project.
  • Incorporate dependencies, such as other projects, assets, and artifacts into a build.

For more information, see CMake: Importing and Exporting Guide and CMake: Using Dependencies Guide.

Import existing 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.

Use dependencies

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

Find CMake module files

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.

Copyright © The Qt Company Ltd. and other contributors. 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.