Cmake Find_Package Specify Path

cmake find_package specify path

In the find_package documentation you have that you can set a path to be searched with PATHS you were missing the S... also you can do something like:

find_package (<package> PATHS paths... NO_DEFAULT_PATH)
find_package (<package>)

Which will check for the path you wrote first, the if it is found it will set found to true and the second instruction will be skipped.

Also, you can use the EXACT option to match an specific version, in case it tries to select 3.4 due to being a newer version.

find_package(OpenCV 3.1 EXACT REQUIRED PATHS /usr/local/opencv3.1)

I hope this helps, if not, write a comment

CMake set start path for FIND_PACKAGE?

You can either set OPENSSL_ROOT_DIR cmake variable or OPENSSL_ROOT_DIR env variable to the following path: /usr/local/Cellar/openssl/* and then use find_package. Example:

set(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/*)
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})

What use is find_package() when you need to specify CMAKE_MODULE_PATH?

Command find_package has two modes: Module mode and Config mode. You are trying to
use Module mode when you actually need Config mode.

Module mode

Find<package>.cmake file located within your project. Something like this:

CMakeLists.txt
cmake/FindFoo.cmake
cmake/FindBoo.cmake

CMakeLists.txt content:

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

Note that CMAKE_MODULE_PATH has high priority and may be usefull when you need to rewrite standard Find<package>.cmake file.

Config mode (install)

<package>Config.cmake file located outside and produced by install
command of other project (Foo for example).

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)

Simplified version of config 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 project installed in 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

Config mode (use)

Use find_package(... CONFIG) to include FooConfig.cmake with imported target foo:

> cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)
project(Boo)

# import library target `foo`
find_package(Foo CONFIG REQUIRED)

add_executable(boo Boo.cpp Boo.hpp)
target_link_libraries(boo foo)
> cmake -H. -B_builds -DCMAKE_VERBOSE_MAKEFILE=ON
> cmake --build _builds
Linking CXX executable Boo
/usr/bin/c++ ... -o Boo /usr/local/lib/libfoo.a

Note that imported target is highly configurable. See my answer.

Update

  • Example

How to set a specific package path for CMakeLists.txt

CMake find_package() finds and configure project dependencies in CMake using two modes of operation, Module and Config, as described in its documentation.

In Module mode (not your case), it looks for a Find<package>.cmake file inside CMAKE_MODULE_PATH. This file searches for header files and libraries and set the necessary CMake variables in case of success.

In your case it is using Config, as requested by the CONFIG keyword. In Config mode CMake looks for for a <name>Config.cmake file or <lowercase-name>-config.cmake.
These config files describe the version of the dependency and the location of header files and library modules.

So you should look for OpenCVConfig.cmake or opencv-config.cmake in CMAKE_PREFIX_PATH or in OpenCV_DIR.
Please note that you have to set(OpenCV_DIR ...) before calling find_package().

cmake find_package relative path in CMakeLists.txt subfolder

You can get the global path in MYLIBConfig.cmake with CMAKE_CURRENT_LIST_DIR:

set(MYLIB_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include/)

Or if you want a relative path to the currently processed source you can get it accordingly with file:

file(RELATIVE_PATH MYLIB_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_LIST_DIR}/include/)


Related Topics



Leave a reply



Submit