首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用cmake FetchContent或任何更简单的解决方案直接从github使用Boost库?

如何使用cmake FetchContent或任何更简单的解决方案直接从github使用Boost库?
EN

Stack Overflow用户
提问于 2022-07-08 14:57:36
回答 2查看 1.1K关注 0票数 2

假设我们希望在cmake多平台项目(ios、macos、android、linux)中使用boost::file_system库。一种方法是直接将boost源代码复制到我们的项目中。它增加了项目的规模,并增加了许多维护问题、修补、更新等。如果我们在cmake 配置步骤中下载boost源代码会怎么样?因此,我添加了最小的示例(file - main.cxx):

代码语言:javascript
运行
复制
    #include <boost/filesystem.hpp>

    #include <iostream>

    int main(int, char**)
    {
        std::cout << boost::filesystem::current_path() << std::endl;
        return std::cout.fail();
    }

接下来是完整的CMakeLists.txt文件,可以在没有将boost安装到系统的情况下从源代码构建这个最小的示例。

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.20...3.23)

project(19-boost-file-system CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_ENABLE_EXPORTS OFF)

include(FetchContent)

fetchcontent_declare(
    BoostAssert GIT_REPOSITORY https://github.com/boostorg/assert.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostConfig GIT_REPOSITORY https://github.com/boostorg/config.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostContainerHash
    GIT_REPOSITORY https://github.com/boostorg/container_hash.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostCore GIT_REPOSITORY https://github.com/boostorg/core.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostDetail GIT_REPOSITORY https://github.com/boostorg/detail.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostInteger GIT_REPOSITORY https://github.com/boostorg/integer.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostStaticAssert
    GIT_REPOSITORY https://github.com/boostorg/static_assert.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostThrowException
    GIT_REPOSITORY https://github.com/boostorg/throw_exception.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostTypeTraits GIT_REPOSITORY https://github.com/boostorg/type_traits.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostPreprocessor
    GIT_REPOSITORY https://github.com/boostorg/preprocessor.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostIterator GIT_REPOSITORY https://github.com/boostorg/iterator.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(BoostIo GIT_REPOSITORY https://github.com/boostorg/io.git
                     GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostConceptCheck
    GIT_REPOSITORY https://github.com/boostorg/concept_check.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostConversion GIT_REPOSITORY https://github.com/boostorg/conversion.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostFunctionTypes
    GIT_REPOSITORY https://github.com/boostorg/function_types.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostFusion GIT_REPOSITORY https://github.com/boostorg/fusion.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(BoostMpl GIT_REPOSITORY https://github.com/boostorg/mpl.git
                     GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostOptional GIT_REPOSITORY https://github.com/boostorg/optional.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostSmartPtr GIT_REPOSITORY https://github.com/boostorg/smart_ptr.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostUtility GIT_REPOSITORY https://github.com/boostorg/utility.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostTypeof GIT_REPOSITORY https://github.com/boostorg/typeof.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostTuple GIT_REPOSITORY https://github.com/boostorg/tuple.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostPredef GIT_REPOSITORY https://github.com/boostorg/predef.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostMove GIT_REPOSITORY https://github.com/boostorg/move.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostAtomic GIT_REPOSITORY https://github.com/boostorg/atomic.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostVariant2 GIT_REPOSITORY https://github.com/boostorg/variant2.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostAlign GIT_REPOSITORY https://github.com/boostorg/align.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostMp11 GIT_REPOSITORY https://github.com/boostorg/mp11.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostWinapi GIT_REPOSITORY https://github.com/boostorg/winapi.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostSystem GIT_REPOSITORY https://github.com/boostorg/system.git
    GIT_TAG boost-1.79.0)
fetchcontent_declare(
    BoostFileSystem GIT_REPOSITORY https://github.com/boostorg/filesystem.git
    GIT_TAG boost-1.79.0)

fetchcontent_makeavailable(
    BoostAssert
    BoostConfig
    BoostInteger
    BoostStaticAssert
    BoostThrowException
    BoostTypeTraits
    BoostPreprocessor
    BoostIo
    BoostIterator
    BoostConceptCheck
    BoostConversion
    BoostFunctionTypes
    BoostFusion
    BoostMpl
    BoostOptional
    BoostSmartPtr
    BoostUtility
    BoostTypeof
    BoostTuple
    BoostPredef
    BoostMove
    BoostAlign
    BoostMp11
    BoostWinapi
    BoostContainerHash
    BoostCore
    BoostDetail
    BoostAtomic
    BoostVariant2
    BoostSystem
    BoostFileSystem)

add_executable(main_boost main.cxx)
target_link_libraries(main_boost PRIVATE Boost::filesystem)

你知道更简单的解决方案吗?如何使用cmake直接从GitHub编译和链接Boost库而不使用任何hussle?

EN

回答 2

Stack Overflow用户

发布于 2022-10-03 10:01:49

这就是我所用的:

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.24)
project(p)

set(BOOST_INCLUDE_LIBRARIES thread filesystem system program_options)
set(BOOST_ENABLE_CMAKE ON)

include(FetchContent)
FetchContent_Declare(
  Boost
  GIT_REPOSITORY https://github.com/boostorg/boost.git
  GIT_TAG boost-1.80.0
)
FetchContent_MakeAvailable(Boost)

add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test PRIVATE Boost::filesystem
                                         Boost::program_options)

这并不完全清楚原因,但在您的情况下,您可以将版本缩减为:

代码语言:javascript
运行
复制
cmake_minimum_required(VERSION 3.24)
project(p)

set(BOOST_INCLUDE_LIBRARIES thread filesystem system)
set(BOOST_ENABLE_CMAKE ON)

include(FetchContent)
FetchContent_Declare(
  Boost
  GIT_REPOSITORY https://github.com/boostorg/boost.git
  GIT_TAG boost-1.74.0
)
FetchContent_MakeAvailable(Boost)

add_executable(boost_test boost_test.cpp)
target_link_libraries(boost_test PRIVATE Boost::filesystem)

另请参阅:

票数 3
EN

Stack Overflow用户

发布于 2022-07-31 13:49:13

您可以尝试使用GIT_SUBMODULES of FetchContent_Declare命令。下面的示例将Boost::线程库添加到测试项目中。

代码语言:javascript
运行
复制
include(FetchContent)

# BOOST

set(BOOST_REQD_SUBMODULES
    "tools/cmake;"
    "libs/assert;libs/exception;libs/throw_exception;libs/static_assert;"
    "libs/bind;libs/function_types;libs/function;"
    "libs/chrono;libs/date_time;"
    "libs/concept_check;"
    "libs/config;libs/container;libs/container_hash;"
    "libs/iterator;libs/utility;libs/type_traits;libs/algorithm;;libs/conversion;libs/numeric/conversion;libs/regex;libs/unordered;libs/tokenizer;libs/move;libs/ratio;libs/lexical_cast;"
    "libs/tuple;libs/variant2;libs/typeof;libs/detail;libs/array;libs/type_index;libs/range;libs/optional;libs/smart_ptr;libs/integer;libs/rational;"
    "libs/intrusive;libs/io;"
    "libs/core;libs/align;libs/predef;libs/preprocessor;libs/system;libs/winapi;libs/atomic;"
    "libs/mpl;libs/fusion;libs/mp11;"
    "libs/thread"
)

Set(FETCHCONTENT_QUIET FALSE)
FetchContent_Declare(
    boost
    GIT_REPOSITORY "https://github.com/boostorg/boost.git"
    GIT_TAG master
    GIT_SUBMODULES ${BOOST_REQD_SUBMODULES}
    GIT_PROGRESS TRUE
    CONFIGURE_COMMAND ""  # tell CMake it's not a cmake project
)

FetchContent_MakeAvailable(boost)

# TEST

ADD_EXECUTABLE(test test.cpp)
TARGET_LINK_LIBRARIES(test PRIVATE Boost::thread gtest gmock gtest_main)

我还在这里找到了BOOST_INCLUDE_LIBRARIES的一个例子,https://hreniuc.dev/use-boost-without-conan-only-with-cmake。但是我没有用过这种方法,所以我不知道它是否真的有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72913306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档