c++ - 强制 cmake 使用旧的 bo​​ost 库

标签 c++ boost cmake libraries

我想使用点云库编译一个简单的项目。不幸的是,它使用了我目前通过官方存储库安装的旧版本的 boost 库。

所以我在 /opt/oldlibs/boost/libs 中安装了旧库。我尝试在 CMakeLists.txt 中设置这个新路径,但不知为何 cmake 仍然使用安装在 /usr/lib 中的当前版本。我是否在我的 CMakeLists.txt 文件中做错了什么?

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
cmake_policy(SET CMP0053 OLD)

project(cloud_viewer)

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

# Add old boost library
link_directories("/usr/oldlibs/libs")
set(Boost_NO_SYSTEM_PATHS TRUE)
if (Boost_NO_SYSTEM_PATHS)
  set(BOOST_ROOT "/opt/oldlibs/boost")
  set(BOOST_INCLUDE_DIRS "/usr/include/boost")
  set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/libs")
endif (Boost_NO_SYSTEM_PATHS)
find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options serialization iostreams chrono atomic)
include_directories(${BOOST_INCLUDE_DIRS})

add_executable (cloud_viewer cloud_viewer.cpp)
target_link_libraries (cloud_viewer ${PCL_LIBRARIES})

cmake 的(部分)输出是:

-- Boost version: 1.64.0
-- Found the following Boost libraries:
--   regex
--   date_time
--   system
--   filesystem
--   thread
--   graph
--   program_options
--   serialization
--   iostreams
--   chrono
--   atomic

但我需要 1.63。我做错了什么?我使用 arch linux,我当前的 cmake 版本是 3.8.2。

编辑

我更新了 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
cmake_policy(SET CMP0053 OLD)

project(cloud_viewer)

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

# Add old boost library
set(BOOST_VERSION_REQUIRED "1.63.0")

set(Boost_NO_SYSTEM_PATHS TRUE)
if (Boost_NO_SYSTEM_PATHS)
  set(BOOST_ROOT "/opt/oldlibs/boost")
  set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/include")
  set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/lib")
endif (Boost_NO_SYSTEM_PATHS)

include_directories(${BOOST_INCLUDE_DIRS})
find_package(Boost ${BOOST_VERSION_REQUIRED} EXACT REQUIRED regex date_time system filesystem thread graph program_options serialization iostreams chrono atomic)

add_executable (cloud_viewer cloud_viewer.cpp)
target_link_libraries (cloud_viewer ${PCL_LIBRARIES})

Cmake 现在知道我想使用旧版本,但仍然找不到它。我又犯错了吗?我通过从 /var/cache/pacman/pkg/' 中提取由 packman 缓存的版本并将其提取到 /opt/oldlibs/boost/` 来安装旧包。

usr 目录如下所示:

usr
├── oldlib
│   ├── boost
│   │   ├── bin
│   │   ├── include (contains all header files)
│   │   ├── lib (contains all libraries, static and shared)
│   │   └── share

这是 cmake 输出的一部分:

CMake Error at /usr/share/cmake-3.8/Modules/FindBoost.cmake:1842 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.64.0

  Boost include path: /usr/include

  Detected version of Boost is too new.  Requested version was 1.63.
Call Stack (most recent call first):
  CMakeLists.txt:23 (find_package)

我还尝试使用选项 -DCMAKE_PREFIX_PATH=/opt/oldlibs/boost/lib 运行 cmake 并改变路径。它没有改变任何东西。

最佳答案

您的 find_package() 似乎有误,因为您没有指定版本要求:

find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options serialization iostreams chrono atomic)

这向 cmake 请求 Boost 并理解“任何 Boost 都可以”。这个假设不成立,你知道的,所以你应该指定你想要的版本:

set(BOOST_VERSION_REQUIRED "your version value here") # adjust as needed
find_package(Boost ${BOOST_VERSION_REQUIRED} REQUIRED regex date_time system filesystem thread graph program_options serialization iostreams chrono atomic)

如果这仍然不起作用(因为 cmake 的版本控制约定导致它在更新更好的假设下更喜欢更新的 Boost...),那么您可以指定 EXACT 强制版本。

set(BOOST_VERSION_REQUIRED "your version value here") # adjust as needed
find_package(Boost ${BOOST_VERSION_REQUIRED} EXACT REQUIRED regex date_time system filesystem thread graph program_options serialization iostreams chrono atomic)

这就是全部 documented here .

关于c++ - 强制 cmake 使用旧的 bo​​ost 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484005/

相关文章:

c++ - std::is_constant_evaluate 行为

c++ - Linux 系统调用 aio_write() 失败,错误代码为 22 (EINVAL)

c++ - CLion 中 CMake 的引用 boost

c++ - C++11 正则表达式中有 match_partial 吗?

git - 如何在 CMake 中使其可执行之前检查文件是否存在

c++ - 如何避免强制转换运算符() 和访问运算符[] 冲突?

c++ - 跨多个函数访问类指针

c++ - 在容器中存储 boost::function 对象

winapi - CMake 找不到 Windows SDK 8.1

c++ - CMake在编译使用OpenCV的项目时遇到问题