CMake - 在顶级CMakeLists.txt中定义包含路径和库

时间:2014-09-08 17:02:35

标签: c++ cmake

我的源代码树如下所示:

--- src/CMakeLists.txt
--- src/MoonLanding/
----- CMakeLists.txt
----- main.cpp

要在MoonLanding中构建src/Moonlanding/项目,请执行以下操作

cmake . -DGFLAGS_INCLUDE_DIR=${CBVR_DEPENDENCIES}/gflags/build/include \
  -DGFLAGS_LIBRARY=${CBVR_DEPENDENCIES}/gflags/build/lib/libgflags.a \
  -DAtlas_CBLAS_INCLUDE_DIR=${CBVR_DEPENDENCIES}/ATLAS-install/include \
  -DAtlas_CBLAS_LIBRARY=${CBVR_DEPENDENCIES}/ATLAS-install/lib/libcblas.a \
  -DAtlas_CLAPACK_INCLUDE_DIR=${CBVR_DEPENDENCIES}/ATLAS-install/include \
  -DAtlas_LAPACK_LIBRARY=${CBVR_DEPENDENCIES}/ATLAS-install/lib/liblapack.a \
  -DAtlas_BLAS_LIBRARY=${CBVR_DEPENDENCIES}/ATLAS-install/lib/libatlas.a \
  -DLMDB_INCLUDE_DIR=${CBVR_DEPENDENCIES}/mdb/libraries/liblmdb \
  -DLMDB_LIBRARIES=${CBVR_DEPENDENCIES}/mdb/libraries/liblmdb/liblmdb.so \
  -DBOOST_ROOT=${CBVR_DEPENDENCIES}/boost_1_56_0/

上面对cmake的调用效果很好,MoonLanding编译。

但是,我想在cmake .下调用src/。为此,我将src/CMakeLists.txt定义为:

set(GFLAGS_INCLUDE_DIR $ENV{CBVR_DEPENDENCIES}/gflags/build/include)
set(GFLAGS_LIBRARY $ENV{CBVR_DEPENDENCIES}/gflags/build/lib/libgflags.a)
set(Atlas_CBLAS_INCLUDE_DIR $ENV{CBVR_DEPENDENCIES}/ATLAS-install/include)
set(Atlas_CBLAS_LIBRARY $ENV{CBVR_DEPENDENCIES}/ATLAS-install/lib/libcblas.a)
set(Atlas_CLAPACK_INCLUDE_DIR $ENV{CBVR_DEPENDENCIES}/ATLAS-install/include)
set(Atlas_LAPACK_LIBRARY $ENV{CBVR_DEPENDENCIES}/ATLAS-install/lib/liblapack.a)
set(Atlas_BLAS_LIBRARY $ENV{CBVR_DEPENDENCIES}/ATLAS-install/lib/libatlas.a)
set(LMDB_INCLUDE_DIR $ENV{CBVR_DEPENDENCIES}/mdb/libraries/liblmdb)
set(LMDB_LIBRARIES $ENV{CBVR_DEPENDENCIES}/mdb/libraries/liblmdb/liblmdb.so)
set(BOOST_ROOT $ENV{CBVR_DEPENDENCIES}/boost_1_56_0/)

add_subdirectory(Moonlanding)
然后,CMake给了我错误:

CMake Error at /usr/local/share/cmake-3.0/Modules/FindPackageHandleStandardArgs.cmake:136 (message):
  Could NOT find LMDB (missing: LMDB_INCLUDE_DIR LMDB_LIBRARIES)

如果我再次调用cmake,错误就会消失。总的来说,我需要为cmake .调用src/Moonlanding三次,以便从src/进行编译。为什么会这样,是否有更清洁的方式来实现这个项目的建设?

(我之所以要分离GFLAGS_INCLUDE_DIR和其他指令,是为了帮助自动化用户的构建过程。)

1 个答案:

答案 0 :(得分:3)

从命令行设置变量和cmake set()命令之间的唯一区别(适用于您的情况)是默认缓存策略。

您最有可能通过缓存值来解决问题:

set(GFLAGS_INCLUDE_DIR $ENV{CBVR_DEPENDENCIES}/gflags/build/include CACHE PATH "Some comment here")
# other sets with "CACHE" addition

PS。在cmake源中每次与缓存相关的更改后,不要忘记进行干净的构建