Imported Targets & Find Modules
An imported target represents a library that already exists on disk. CMake doesn't build it; it only knows where the file is and what consumers need (include paths, definitions, dependencies). Every find_package worth using gives you one, and when a library ships no CMake support at all, you can write the target, or a whole Find module, yourself.
add_library(... IMPORTED)
add_library(Tiny::json STATIC IMPORTED)
set_target_properties(Tiny::json PROPERTIES
IMPORTED_LOCATION "/opt/tinyjson/lib/libtinyjson.a"
INTERFACE_INCLUDE_DIRECTORIES "/opt/tinyjson/include")IMPORTED_LOCATIONis the library file (for a SHARED import on Windows,IMPORTED_IMPLIBis the.libimport library).- Usage requirements are set directly as
INTERFACE_*properties; an imported target has no build requirements, because nothing is built. IMPORTED_LOCATION_<CONFIG>andIMPORTED_CONFIGURATIONShold different files for Debug and Release, which is what installed packages generate.- Imported targets are visible only in the directory that created them and below, unless created with
IMPORTED GLOBAL.
Writing a Find module
Suppose a prebuilt library tinyjson is unpacked in a vendor folder, with no CMake files. A Find module searches for it and wraps it in an imported target. This session first builds the "prebuilt" library by hand with g++ and ar, then writes FindTinyJson.cmake:
# --- a prebuilt library someone gave us: headers + .a, no CMake files
mkdir -p vendor/tinyjson/include vendor/tinyjson/lib
cat > vendor/tinyjson/include/tinyjson.h <<'SRC'
#pragma once
#define TINYJSON_VERSION "0.9.2"
const char* tinyjson_quote(const char* s);
SRC
cat > tj.cpp <<'SRC'
#include <string>
#include "tinyjson.h"
const char* tinyjson_quote(const char* s) { static std::string r; r = std::string("\"") + s + "\""; return r.c_str(); }
SRC
g++ -c -Ivendor/tinyjson/include tj.cpp -o tj.o && ar rcs vendor/tinyjson/lib/libtinyjson.a tj.o && rm tj.*
# --- our project, with a Find module for it
mkdir -p app/cmake && cd app
cat > cmake/FindTinyJson.cmake <<'CM'
find_path(TinyJson_INCLUDE_DIR NAMES tinyjson.h PATH_SUFFIXES include)
find_library(TinyJson_LIBRARY NAMES tinyjson PATH_SUFFIXES lib)
if(TinyJson_INCLUDE_DIR AND EXISTS "${TinyJson_INCLUDE_DIR}/tinyjson.h")
file(STRINGS "${TinyJson_INCLUDE_DIR}/tinyjson.h" _ver REGEX "#define TINYJSON_VERSION ")
string(REGEX REPLACE ".*\"(.*)\".*" "\\1" TinyJson_VERSION "${_ver}")
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(TinyJson
REQUIRED_VARS TinyJson_LIBRARY TinyJson_INCLUDE_DIR
VERSION_VAR TinyJson_VERSION)
if(TinyJson_FOUND AND NOT TARGET TinyJson::TinyJson)
add_library(TinyJson::TinyJson UNKNOWN IMPORTED)
set_target_properties(TinyJson::TinyJson PROPERTIES
IMPORTED_LOCATION "${TinyJson_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${TinyJson_INCLUDE_DIR}")
endif()
mark_as_advanced(TinyJson_INCLUDE_DIR TinyJson_LIBRARY)
CM
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(App LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
find_package(TinyJson 0.9 REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE TinyJson::TinyJson)
CM
cat > main.cpp <<'SRC'
#include <cstdio>
#include <tinyjson.h>
int main() { std::printf("tinyjson %s: %s\n", TINYJSON_VERSION, tinyjson_quote("hi")); }
SRC
echo "--- without a hint"
cmake -S . -B build -G Ninja --log-level=NOTICE
echo "--- with TinyJson_ROOT"
cmake -S . -B build -G Ninja -DTinyJson_ROOT=$HOME/vendor/tinyjson 2>&1 | grep -i tinyjson
cmake --build build > /dev/null && ./build/app--- without a hint
CMake Error at /opt/cmake/share/cmake-4.4/Modules/FindPackageHandleStandardArgs.cmake:290 (message):
Could NOT find TinyJson (missing: TinyJson_LIBRARY TinyJson_INCLUDE_DIR)
(Required is at least version "0.9")
Call Stack (most recent call first):
/opt/cmake/share/cmake-4.4/Modules/FindPackageHandleStandardArgs.cmake:654 (_FPHSA_FAILURE_MESSAGE)
cmake/FindTinyJson.cmake:10 (find_package_handle_standard_args)
CMakeLists.txt:4 (find_package)
-- Configuring incomplete, errors occurred!
--- with TinyJson_ROOT
-- Found TinyJson: /home/learner/vendor/tinyjson/lib/libtinyjson.a (found suitable version "0.9.2", minimum required is "0.9")
tinyjson 0.9.2: "hi"
The pieces of a good Find module:
find_path/find_librarysearch the standard locations plus the hints, and store the result in cache variables (so users can override them with-DTinyJson_LIBRARY=/path/libtinyjson.a).TinyJson_ROOTworks automatically becausefind_packageadds<Name>_ROOTto the search for everyfind_*call inside the module (policy CMP0074).- The version is read from the header.
find_package_handle_standard_argssetsTinyJson_FOUND, checks the requested version, prints the standardFound…/Could NOT find…lines, and fails whenREQUIREDwas given.- The imported target is created only once (
NOT TARGET).UNKNOWNmeans "static or shared, I don't care": CMake links the file it found either way.
The first attempt shows the failure path: the standard message names the cache variables that are still missing and the version that was required, so the user knows what to set. The second attempt added a hint and found the library and its version.
When to write one
- Prefer, in this order: the package's own config file (
find_package(Foo CONFIG)), a pkg-config file viapkg_check_modules(... IMPORTED_TARGET ...), a Find module that ships with CMake (CMake 4.4 ships about 160, such asFindZLIB,FindOpenSSLandFindCURL), and only then your own. - A package manager (C/C++ Packages: Conan, vcpkg) generates config files for everything it installs, which removes most of the need for hand-written Find modules.
Test yourself
Why does an imported target have only INTERFACE_* properties for its usage?
- CMake limitation
- Nothing is built for it, so there are no build requirements, only what consumers need
- INTERFACE means header-only
- To hide it from consumers
Why do find_path and find_library store results in cache variables?
- For speed only
- So the user can override a wrong or missing result with -D, and repeated configures don't search again
- Cache variables are required by IMPORTED targets
- They don't
Which module function prints Found X / Could NOT find X (missing: ...) and sets X_FOUND?
find_dependencyfind_package_handle_standard_argspkg_check_modulescheck_required_components