【问题标题】:Use Boost as a git submodule with CMake使用 Boost 作为 CMake 的 git 子模块
【发布时间】:2021-11-22 12:11:20
【问题描述】:

我想在我的 C++ 项目中使用 Boost 库(更准确地说,我对 Boost Graph Library 感兴趣)。我希望它在我的 git 存储库中,作为一个 git 子模块,因为它对所有其他依赖项都是如此。

例如,如果我想以 fmt 依赖项作为 git 子模块启动一个项目,我会这样做:

mkdir my_project
cd my_project
git init .

然后,我想在标签8.0.0 上添加fmt 作为子模块:

mkdir deps
git submodule add https://github.com/fmtlib/fmt.git deps/fmt
cd deps/fmt
git checkout 8.0.0

然后,我回到我的项目的根文件夹:

cd ../..

我创建了以下文件:

  • main.cpp
#include <fmt/format.h>

int main() {
    fmt::print("Hello, World!\n");
    return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(test_boost)

add_subdirectory(deps/fmt)

add_executable(test_boost main.cpp)
target_link_libraries(test_boost fmt::fmt)

然后,我们就可以构建了:

mkdir build
cd build
cmake ..
make

并且二进制文件工作正常,预计会打印Hello, World!,这很棒。

现在如果我想添加 boost,版本 1.77.0:

git submodule add https://github.com/boostorg/boost deps/boost
git submodule update --init --recursive  # To get Boost's own submodules.
cd deps/boost
git checkout boost-1.77.0
cd ../..

现在我想使用这个 boost 文件夹作为我项目的依赖项,这就是它变得棘手的地方。我从1.77 版本中读到here,我应该可以使用find_package() 来执行此操作,因为它废弃了FindBoost 的东西:

find_package(Boost 1.77.0 REQUIRED CONFIG PATHS deps/boost/tools/boost_install)

但我收到以下错误:

-- Module support is disabled.
-- Version: 8.0.1
-- Build type: Debug
-- CXX_STANDARD: 11
-- Required features: cxx_variadic_templates
CMake Error at CMakeLists.txt:6 (find_package):
  Could not find a configuration file for package "Boost" that is compatible
  with requested version "1.77.0".

  The following configuration files were considered but not accepted:

    /home/me/tests/boost_so/my_project/deps/boost/tools/boost_install/BoostConfig.cmake, version: unknown



-- Configuring incomplete, errors occurred!
See also "/home/me/tests/boost_so/my_project/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "/home/me/tests/boost_so/my_project/cmake-build-debug/CMakeFiles/CMakeError.log".

我也尝试了其他方法,但遇到以下错误之一:

  • 同上一个。
  • CMake 使用我从 /usr/local 安装的 Boost,这不是我想要的。

我正在使用 CMake 3.17.2。有可能这样做还是我错过了什么?

【问题讨论】:

  • 你可能需要在find_package 工作之前建立我想象的提升?使用像 conan 或 vcpkg 这样的包管理器可能会让你的生活更轻松
  • 我不需要用add_subdirectory(deps/fmt) 构建fmt,它是在我的项目构建过程中根据需要构建的,所以我很困惑......
  • 如果没有安装 Boost,您将无法使用find_package(Boost)。顺便说一句,您可以打开文件 BoostConfig.cmake 并找到以下行:“# 此 CMake 配置文件,作为 b2 install 完成的 Boost 构建和安装过程的一部分安装,提供对 find_package(Boost) 的支持。”如果您只需要 Boost 的仅标头部分,则可以添加适当的包含目录。
  • ...不要。这不值得痛苦 IME。使用 vcpkg 或柯南。

标签: c++ git boost cmake git-submodules


【解决方案1】:

那个link 还列出了你需要使用的Cmake 版本。为了能够使用FindBoost,您需要cmake 版本3.21.3 或更高版本,但您使用的是3.17.2

【讨论】:

    【解决方案2】:

    我找到了一个看起来很简单的解决方案。您可以只使用add_subdirectory(),如fmt 所示。

    完整的CMakeLists.txt 文件如下所示:

    cmake_minimum_required(VERSION 3.17)
    project(test_boost)
    
    add_subdirectory(deps/fmt)
    add_subdirectory(deps/boost EXCLUDE_FROM_ALL)
    
    add_executable(test_boost main.cpp)
    target_link_libraries(test_boost fmt::fmt Boost::graph)
    

    然后,您可以看到the following 编译并运行良好:

    #include <fmt/format.h>
    #include <boost/graph/adjacency_list.hpp>
    #include <array>
    #include <utility>
    #include <iostream>
    
    int main() {
        fmt::print("Hello, World!\n");
    
        enum { topLeft, topRight, bottomRight, bottomLeft };
    
        std::array<std::pair<int, int>, 4> edges{{
                                                         std::make_pair(topLeft, topRight),
                                                         std::make_pair(topRight, bottomRight),
                                                         std::make_pair(bottomRight, bottomLeft),
                                                         std::make_pair(bottomLeft, topLeft)
                                                 }};
    
        typedef boost::adjacency_list<boost::setS, boost::vecS,
                boost::undirectedS> graph;
        graph g{edges.begin(), edges.end(), 4};
    
        std::cout << boost::num_vertices(g) << '\n';
        std::cout << boost::num_edges(g) << '\n';
    
        g.clear();
        return 0;
    }
    

    哪些输出

    Hello, World!
    4
    4
    

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2011-07-12
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多