Directory Search in CMake

Problem:

I want to add numeric binding formatting as an include directory in my assembly. This is usually compiled as:

c++ -I/where/you/want/to/install/it/include/boost-numeric-bindings

All the header files that I link from my program belong to this directory, so in CMake I want to find this directory (wherever it is installed in the parent system) and add it to include_directories .

What I'm looking for:

Something like that:

 find_directory(BNB_INCLUDE_DIR boost-numeric-bindings) include_directories(${BNB_INCLUDE_DIR}) 

But the find_directory command find_directory not exist. Did I miss something?

What I tried:

I tried:

 find_path(BNB_INCLUDE_DIR boost/numeric/bindings/traits/ublas_vector.hpp) include_directories(${BNB_INCLUDE_DIR}) 

(this is the first file I need from the library), but it gives me the full path to the file, and not the path to the directory containing the entire prefix, to the file specified in the command.

+6
source share
1 answer

See this answer for information on how to write a cmake search file. For example, here is what I wrote for the lm sensor library:

 # - Try to find the LM_SENSORS library. # # The following are set after configuration is done: # LM_SENSORS_FOUND # LM_SENSORS_INCLUDE_DIRS # LM_SENSORS_LIBRARY_DIRS # LM_SENSORS_LIBRARIES find_path(LM_SENSORS_INCLUDE_DIR NAMES sensors/sensors.h) find_library(LM_SENSORS_LIBRARY NAMES libsensors sensors) message("LM_SENSORS include dir = ${LM_SENSORS_INCLUDE_DIR}") message("LM_SENSORS lib = ${LM_SENSORS_LIBRARY}") set(LM_SENSORS_LIBRARIES ${LM_SENSORS_LIBRARY}) set(LM_SENSORS_INCLUDE_DIRS ${LM_SENSORS_INCLUDE_DIR}) include(FindPackageHandleStandardArgs) # Handle the QUIETLY and REQUIRED arguments and set the LM_SENSORS_FOUND to TRUE # if all listed variables are TRUE find_package_handle_standard_args(LM_SENSORS DEFAULT_MSG LM_SENSORS_LIBRARY LM_SENSORS_INCLUDE_DIR) mark_as_advanced(LM_SENSORS_INCLUDE_DIR LM_SENSORS_LIBRARY) 

Change the above to match your library ( boost-numeric-bindings ), name the file Findboost-numeric-bindings.cmake and put it in the cmake module directory (or create one of them in the source tree).

Then in the CMakeLists.txt file do the following:

 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} your_cmake_module_dir) find_package (boost-numeric-bindings REQUIRED) include_directories(${BOOST_NUMERIC_BINDINGS_INCLUDE_DIR}) 

Then, assuming you don’t have the library installed in the standard location, run cmake as follows:

cmake -D CMAKE_PREFIX_PATH:STRING="/where/you/have/installed/it/" <source path>

Edit

Before calling find_path or find_package make sure you define a project. Otherwise, CMAKE_SYSTEM_INCLUDE_PATH will not be set. For instance:

 find_path (BOOST_STATE_HPP boost/statechart/state.hpp) message ("CMAKE_FIND_ROOT_PATH=${CMAKE_FIND_ROOT_PATH}") message ("CMAKE_SYSTEM_INCLUDE_PATH=${CMAKE_SYSTEM_INCLUDE_PATH}") message ("CMAKE_SYSTEM_FRAMEWORK_PATH=${CMAKE_SYSTEM_FRAMEWORK_PATH}") message ("CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}") message ("BOOST_STATE_HPP=${BOOST_STATE_HPP}") project (my_project) 

will result in the following cmake output:

 CMAKE_FIND_ROOT_PATH= CMAKE_SYSTEM_INCLUDE_PATH= CMAKE_SYSTEM_FRAMEWORK_PATH= CMAKE_PREFIX_PATH= BOOST_STATE_HPP=BOOST_STATE_HPP-NOTFOUND 

then like this:

 project (my_project) find_path (BOOST_STATE_HPP boost/statechart/state.hpp) message ("CMAKE_FIND_ROOT_PATH=${CMAKE_FIND_ROOT_PATH}") message ("CMAKE_SYSTEM_INCLUDE_PATH=${CMAKE_SYSTEM_INCLUDE_PATH}") message ("CMAKE_SYSTEM_FRAMEWORK_PATH=${CMAKE_SYSTEM_FRAMEWORK_PATH}") message ("CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}") message ("BOOST_STATE_HPP=${BOOST_STATE_HPP}") 

results in a successful state.hpp search and setting BOOST_STATE_HPP to /usr/include , as you wish:

 CMAKE_FIND_ROOT_PATH= CMAKE_SYSTEM_INCLUDE_PATH=/usr/include/w32api;/usr/X11R6/include;/usr/include/X11;/usr/pkg/include;/opt/csw/include;/opt/include;/usr/openwin/include CMAKE_SYSTEM_FRAMEWORK_PATH= CMAKE_PREFIX_PATH= BOOST_STATE_HPP=/usr/include 
+4
source

Source: https://habr.com/ru/post/955697/


All Articles