The find_package command has two modes: Module mode and Config . You try to use Module mode when you really need Config mode.
Module mode
Find<package>.cmake file located inside your project. Something like that:
CMakeLists.txt cmake/FindFoo.cmake cmake/FindBoo.cmake
CMakeLists.txt :
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") find_package(Foo REQUIRED) # FOO_INCLUDE_DIR, FOO_LIBRARIES find_package(Boo REQUIRED) # BOO_INCLUDE_DIR, BOO_LIBRARIES include_directories("${FOO_INCLUDE_DIR}") include_directories("${BOO_INCLUDE_DIR}") add_executable(Bar Bar.hpp Bar.cpp) target_link_libraries(Bar ${FOO_LIBRARIES} ${BOO_LIBRARIES})
Please note that CMAKE_MODULE_PATH has high priority and can be useful when you need to rewrite the standard Find<package>.cmake file.
Configuration Mode (Installation)
<package>Config.cmake file located outside and created by the install command of another project (for example, Foo ).
Foo library:
> cat CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(Foo) add_library(foo Foo.hpp Foo.cpp) install(FILES Foo.hpp DESTINATION include) install(TARGETS foo DESTINATION lib) install(FILES FooConfig.cmake DESTINATION lib/cmake/Foo)
A simplified version of the configuration file:
> cat FooConfig.cmake add_library(foo STATIC IMPORTED) find_library(FOO_LIBRARY_PATH foo HINTS "${CMAKE_CURRENT_LIST_DIR}/../../") set_target_properties(foo PROPERTIES IMPORTED_LOCATION "${FOO_LIBRARY_PATH}")
By default, the project is installed in the CMAKE_INSTALL_PREFIX directory:
> cmake -H. -B_builds > cmake --build _builds --target install -- Install configuration: "" -- Installing: /usr/local/include/Foo.hpp -- Installing: /usr/local/lib/libfoo.a -- Installing: /usr/local/lib/cmake/Foo/FooConfig.cmake
Configuration Mode (use)
Use find_package(... CONFIG) to enable FooConfig.cmake with the imported target Foo :
> cat CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(Boo)
Note that the imported target is highly customizable. See my answer.
Update
user2288008 Dec 31 '14 at 12:00 2013-12-31 12:00
source share