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.