在CMake中查找目录

时间:2013-10-10 17:38:50

标签: c++ boost build cmake

问题:

我想在我的构建中添加boost numeric bindings作为include目录。这通常编译为:

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

我从程序中引用的所有头文件都是相对于此目录的,所以在CMake中我想找到这个目录(在父系统上安装的任何地方)并将其添加到include_directories

我在寻找:

这样的事情:

find_directory(BNB_INCLUDE_DIR boost-numeric-bindings)

include_directories(${BNB_INCLUDE_DIR})

但是find_directory命令不存在。我在这里错过了什么吗?

我尝试了什么:

我试过了:

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

include_directories(${BNB_INCLUDE_DIR})

(这是我需要从库中获得的第一个文件),但这给了我文件的完整路径,而不是包含命令中指定文件的整个前缀的目录的路径。

1 个答案:

答案 0 :(得分:4)

有关如何编写cmake查找文件的信息,请参阅this answer。举个例子,这是我为lm-sensors库写的一个:

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

更改上面的内容以匹配您的库(boost-numeric-bindings),将文件命名为Findboost-numeric-bindings.cmake,并将其放在您的cmake模块目录中(或在源代码树中创建其中一个)。

然后在CMakeLists.txt文件中,执行以下操作:

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

然后,假设您没有在标准位置安装库,请按如下方式运行cmake:

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

修改

在致电find_pathfind_package之前,请确保您已定义项目。否则将不会设置CMAKE_SYSTEM_INCLUDE_PATH。例如:

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)

将导致以下cmake输出:

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

而这:

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

导致成功找到state.hpp并将BOOST_STATE_HPP设置为/usr/include,如您所愿:

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