Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a directory in CMake

The problem:

I want to add the boost numeric bindings as an include directory in my build. This is typically compiled as:

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

All of the header files I reference from my program are all relative to this directory, so in CMake I want to find this directory (wherever it is installed on the parent system) and add it to the include_directories.

What I'm looking for:

Something like this:

find_directory(BNB_INCLUDE_DIR boost-numeric-bindings)

include_directories(${BNB_INCLUDE_DIR})

But the find_directory command does not exist. Am I missing something here?

What I've tried:

I've tried:

find_path(BNB_INCLUDE_DIR boost/numeric/bindings/traits/ublas_vector.hpp)

include_directories(${BNB_INCLUDE_DIR})

(which is the first file I need from the library) but this 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.

like image 455
Neal Kruis Avatar asked Oct 10 '13 17:10

Neal Kruis


1 Answers

See this answer for information on how to write a cmake Find file. As an example, here is one that I wrote for the lm-sensors 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 your cmake module dir (or create one of these in your source tree).

Then in your CMakeLists.txt file, do this:

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 a standard location, run cmake as follows:

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

Edit

Make sure you have defined a project before you call find_path or find_package. Otherwise CMAKE_SYSTEM_INCLUDE_PATH will not be set. For example:

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

whereas 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 sucessfully finding state.hpp and setting BOOST_STATE_HPP to /usr/include, as you desire:

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
like image 98
Patrick Avatar answered Sep 26 '22 06:09

Patrick