使用 Boost 作为 CMake 的 git 子模块

Und*_*nda 8 c++ git boost cmake git-submodules

我想在我的 C++ 项目中使用 Boost 库(更准确地说,我对 Boost Graph Library 感兴趣)。我希望它作为 git 子模块位于我的 git 存储库中,就像它对所有其他依赖项所做的那样。

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

mkdir my_project
cd my_project
git init .
Run Code Online (Sandbox Code Playgroud)

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

mkdir deps
git submodule add https://github.com/fmtlib/fmt.git deps/fmt
cd deps/fmt
git checkout 8.0.0
Run Code Online (Sandbox Code Playgroud)

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

cd ../..
Run Code Online (Sandbox Code Playgroud)

我创建以下文件:

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

int main() {
    fmt::print("Hello, World!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
  • 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)
Run Code Online (Sandbox Code Playgroud)

然后,我们可以构建:

mkdir build
cd build
cmake ..
make
Run Code Online (Sandbox Code Playgroud)

二进制文件工作正常,可以打印Hello, World!,这很棒。

现在如果我想添加增强版本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 ../..
Run Code Online (Sandbox Code Playgroud)

现在我想使用这个 boost 文件夹作为我的项目的依赖项,这就是它变得棘手的地方。我在这里读到,从 version 1.77,我应该能够使用它find_package()来做到这一点,因为它废弃了这个FindBoost东西:

find_package(Boost 1.77.0 REQUIRED CONFIG PATHS deps/boost/tools/boost_install)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

-- 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".
Run Code Online (Sandbox Code Playgroud)

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

  • 和上面那个是一样的。
  • CMake 使用我的 Boost 安装/usr/local,这不是我想要的。

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

Und*_*nda 4

我找到了一个看起来很简单的解决方案。您可以如图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)
Run Code Online (Sandbox Code Playgroud)

然后,您可以看到以下内容编译并运行良好:

#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;
}
Run Code Online (Sandbox Code Playgroud)

哪个输出

Hello, World!
4
4
Run Code Online (Sandbox Code Playgroud)