[CMake] Explanation of the CMake INSTALL and EXPORT Commands

Matthew Woehlke matthew.woehlke at kitware.com
Thu Apr 4 17:26:30 EDT 2013


On 2013-04-04 15:32, Saad Khattak wrote:
> Thanks for the very valuable info Matthew.
>
> If Project A is installed (to a standard location), then it is available
>> system wide, yes. However you should still use find_package(A) rather
>> than relying on e.g. target_link_libraries(B A)
>
> I tried to use find_package(A) but CMake would display a warning:
>
> "By not providing "FindA.cmake" in CMAKE_MODULE_PATH this project has asked
> CMake to find a package configuration file provided by "A", but CMake did
> not find one"
>
> Obviously I am not installing it correctly. When do I know my
> libraries/package is installed correctly (apart from the fact that
> find_package will be able to find it)? Is there a specific folder that I
> can check to make sure the installation took place properly and that CMake
> will be able to find the libraries/packages? Am I correct in assuming that
> if I do the install correctly, I do not have to write a package
> configuration file and that it will be provided by CMake automatically?

The usual place for <name>Config.cmake to live is in 
/usr/lib[64]/cmake/<name>, although a number of possible locations are 
searched. See documentation of find_package (second form) for a more 
detailed explanation.

You *do* need to write and install a <name>Config.cmake, but for trivial 
cases, the file will probably just set <name>_INCLUDE_DIR[S] (your 
choice, but I prefer DIRS as it "allows"¹ giving more than one) and 
include() its <name>Targets.cmake (per my previous message).

(¹ Not a technical limitation, just Principle of Least Surprise.)

Example:

FooConfig.cmake.in
set(Foo_INCLUDE_DIRS @Foo_INCLUDE_DIRS@)
include("${CMAKE_CURRENT_LIST_DIR}"

CMakeLists.txt
project(Foo)
# ...code to build your project...
install(EXPORTS
   # ...
)

set(Foo_INCLUDE_DIRS "${CMAKE_INSTALL_PREFIX}/include")
configure_file(
   FooConfig.cmake.in
   "${Foo_BINARY_DIR}/FooConfig-install.cmake"
   @ONLY
)
install(
   FILES "${Foo_BINARY_DIR}/FooConfig-install.cmake"
   DESTINATION lib${LIB_SUFFIX}/cmake/Foo
   RENAME FooConfig.cmake
)

# Below this line is optional (only needed if you want to be able to
# use Foo from its build directory:

# Use export() to create "${Foo_BINARY_DIR}/FooTargets.cmake"
set(Foo_INCLUDE_DIRS
   # ...list of include dirs when using Foo from build tree...
)
configure_file(
   FooConfig.cmake.in
   "${Foo_BINARY_DIR}/FooConfig.cmake"
   @ONLY
)

-- 
Matthew



More information about the CMake mailing list