[英]Use Boost as a git submodule with CMake
I want to use the Boost library for my C++ project (more precisely, I'm interested in the Boost Graph Library).我想在我的 C++ 项目中使用 Boost 库(更准确地说,我对 Boost Graph 库感兴趣)。 I'd like it to be inside my git repository, as a git submodule, as it's done for every other dependency.
我希望它在我的 git 存储库中,作为 git 子模块,因为它是为所有其他依赖项完成的。
For example, if I want to start a project with fmt
dependency as a git submodule, I do:例如,如果我想以
fmt
依赖项作为 git 子模块启动一个项目,我会这样做:
mkdir my_project
cd my_project
git init .
Then, I want to add fmt
as a submodule, on tag 8.0.0
:然后,我想在标签
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
Then, I go back to my project's root folder:然后,我回到我的项目的根文件夹:
cd ../..
And I create the following files:我创建了以下文件:
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)
Then, we're able to build:然后,我们可以构建:
mkdir build
cd build
cmake ..
make
And the binary works fine, expectedly printing Hello, World!
二进制工作正常,预计打印
Hello, World!
, which is great. ,这很棒。
Now if I want to add boost, version 1.77.0
:现在,如果我想添加 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 ../..
Now I want to use this boost folder as dependency of my project, and this is where it gets tricky.现在我想使用这个 boost 文件夹作为我项目的依赖项,这就是它变得棘手的地方。 I read here that from version
1.77
, I should be able to use find_package()
to do this, as it obseletes the FindBoost
thing:我在这里读到从版本
1.77
,我应该能够使用find_package()
来做到这一点,因为它淘汰了FindBoost
东西:
find_package(Boost 1.77.0 REQUIRED CONFIG PATHS deps/boost/tools/boost_install)
But I get the following error:但我收到以下错误:
-- 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".
I tried other things as well, but I run into one of the following errors:我也尝试了其他事情,但我遇到了以下错误之一:
/usr/local
, which is not what I want. /usr/local
安装的 Boost ,这不是我想要的。 I'm using CMake 3.17.2
.我正在使用 CMake
3.17.2
。 Is it possible to do this or am I missing something ?有可能这样做还是我错过了什么?
I found a solution which seems quite simple.我找到了一个看起来很简单的解决方案。 You can just
add_subdirectory()
as shown for fmt
.您可以只
add_subdirectory()
如fmt
所示。
The complete CMakeLists.txt
file looks like this:完整的
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)
Then, you can see that the following compiles and runs fine:然后,您可以看到以下编译并运行良好:
#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;
}
Which outputs哪些输出
Hello, World!
4
4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.