• cmka下切换使用不同版本的boost-未解决


    前言

    我大体知道问题在哪了:应该是自己编译的boost没有对应的cmake。需要先解决这个问题。我有时间来解决。

    干活的时候,需要的是1.68版本的boost。我本机安装的1.71版本的boost。
    一些代码和版本密切相关,比如:

    #include <boost/version.hpp>
    #if BOOST_VERSION <= 106800
    #   include <boost/signals.hpp>
    #else
    # include <boost/asio/signal_set.hpp>
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    所以,有必要在一台机器上灵活的切换使用不同的boost版本
    我当前的环境如下所示:

    uname -a
    Linux dacao-VirtualBox 5.11.0-27-generic #29~20.04.1-Ubuntu SMP Wed Aug 11 15:58:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
    
    ➜ cmake --version                                                                                                                                    
    cmake version 3.16.3
    CMake suite maintained and supported by Kitware (kitware.com/cmake).
    
    # sudo apt install libboost1.71-dev-all
    ➜ dpkg -S /usr/include/boost/version.hpp
    libboost1.71-dev:amd64: /usr/include/boost/version.hpp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    而我们编译的程序如下所示,保存为boost_version.cpp。其中BOOST_SCOPE_EXITdeclares a scope exit.

    #include <iostream>
    #include <boost/scope_exit.hpp>
    
    struct Object
    {
        int i;
    };
    
    
    int main(void)
    {
        struct Object* obj = new Object;
        BOOST_SCOPE_EXIT(obj) {
            delete obj;
            std::cout<<"obj has been released"<<std::endl;
        }BOOST_SCOPE_EXIT_END
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在cmake中查找boost库和头文件

    参考:FindBoost - cmake
    我们编写出如下的CMakeLists.txt

    cmake_minimum_required(VERSION 3.11)
    
    project(boost_version)
    
    find_package(
        Boost REQUIRED
        COMPONENTS program_options) # COMPONENTS用不着,为了编译复杂点,加上的
    
    # include_directories(${Boost_INCLUDE_DIRS})
    
    add_executable(${PROJECT_NAME} boost_version.cpp)
    
    # target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    我们启用Boost_DEBUG选项,以看到查找Boost的输出。

    mkdir build && cd build
    ➜  cmake -DBoost_DEBUG=ON ..                                                                                                                          
    -- Found Boost 1.71.0 at /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0
    --   Requested configuration: REQUIRED COMPONENTS program_options
    -- BoostConfig: find_package(boost_headers 1.71.0 EXACT CONFIG REQUIRED  HINTS /usr/lib/x86_64-linux-gnu/cmake)
    -- Found boost_headers 1.71.0 at /usr/lib/x86_64-linux-gnu/cmake/boost_headers-1.71.0
    -- BoostConfig: find_package(boost_program_options 1.71.0 EXACT CONFIG REQUIRED  HINTS /usr/lib/x86_64-linux-gnu/cmake)
    -- Found boost_program_options 1.71.0 at /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0
    -- Boost toolset is gcc9 (GNU 9.4.0)
    -- Scanning /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant*.cmake
    --   Including /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant-shared.cmake
    --   [x] libboost_program_options.so.1.71.0
    --   Including /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant-static.cmake
    --   [ ] libboost_program_options.a
    -- Adding boost_program_options dependencies: headers
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/dacao/exercise/CPlusPlus/build
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    可以看到,我们顺利的cmake中添加了查找boost的功能。

    在cmake中查找boost库和头文件 – 自定义boost库和头文件位置 – 失败 – 未解决

    首先,我们在Boost Version History中下载我们需要的版本。
    然后,按照Getting Started on Unix Variants中的介绍,编译和安装boost。

    wget -c https://boostorg.jfrog.io/artifactory/main/release/1.68.0/source/boost_1_68_0.tar.gz
    ➜  tar -zxvf boost_1_68_0.tar.gz
    ➜  cd boost_1_68_0
    
    ➜  ./bootstrap.sh # 这设置了所有内容并构建`b2`了用于构建 Boost 的程序。
    ➜  ./b2 install --prefix=/home/dacao/exercise/boost #编译并安装到指定位置cd /home/dacao/exercise/boost && ls
    include  lib # 分别存放boost的头文件和库
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    按照FindBoost - cmake的文档介绍:
    This module reads hints about search locations from variables:

    BOOST_ROOT             - Preferred installation prefix
     (or BOOSTROOT)
    BOOST_INCLUDEDIR       - Preferred include directory e.g. <prefix>/include
    BOOST_LIBRARYDIR       - Preferred library directory e.g. <prefix>/lib
    Boost_NO_SYSTEM_PATHS  - Set to ON to disable searching in locations not
                             specified by these hint variables. Default is OFF.
    Boost_ADDITIONAL_VERSIONS
                           - List of Boost versions not known to this module
                             (Boost install locations may contain the version)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    所以,按理说,我们可以使用BOOST_ROOT指定我们的安装位置,使用Boost_NO_SYSTEM_PATHS指定不去查找系统的boost库,即可满足我们的需求。
    然而,现实并非如此:

    ➜  cmake -DBoost_NO_SYSTEM_PATHS=ON -DBOOST_ROOT=/home/dacao/exercise/boost   -DBoost_DEBUG=ON ..
    -- Found Boost 1.71.0 at /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0
    --   Requested configuration: REQUIRED COMPONENTS program_options
    -- BoostConfig: find_package(boost_headers 1.71.0 EXACT CONFIG REQUIRED  HINTS /usr/lib/x86_64-linux-gnu/cmake)
    -- Found boost_headers 1.71.0 at /usr/lib/x86_64-linux-gnu/cmake/boost_headers-1.71.0
    -- BoostConfig: find_package(boost_program_options 1.71.0 EXACT CONFIG REQUIRED  HINTS /usr/lib/x86_64-linux-gnu/cmake)
    -- Found boost_program_options 1.71.0 at /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0
    -- Boost toolset is gcc9 (GNU 9.4.0)
    -- Scanning /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant*.cmake
    --   Including /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant-shared.cmake
    --   [x] libboost_program_options.so.1.71.0
    --   Including /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant-static.cmake
    --   [ ] libboost_program_options.a
    -- Adding boost_program_options dependencies: headers
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/dacao/exercise/CPlusPlus/build
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    并没有去找我们指定的boost库。在CMakeCache.txt可以更加清楚的看到这一点。

    //No help, variable specified on the command line.
    BOOST_ROOT:UNINITIALIZED=/home/dacao/exercise/boost
    
    //No help, variable specified on the command line.
    Boost_DEBUG:UNINITIALIZED=ON
    
    //The directory containing a CMake configuration file for Boost.
    Boost_DIR:PATH=/usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0
    
    //No help, variable specified on the command line.
    Boost_NO_SYSTEM_PATHS:UNINITIALIZED=ON
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    最后

    我在网上搜了一圈,没有找见该咋整。
    我搜到两个相关的问题: How can I get CMake to find my alternative Boost installation?CMake is not able to find BOOST libraries

    如果要解决这个问题(需要时间和足够的耐心),可以分为三步来考虑:

    • 详细了解find_package,阅读查找过程。
    • 分析sudo apt install libboost1.71-dev-all./b2 installl的区别。
    • 结合上面两步,分析出问题根源,并解决问题。

    我们看下第二步,安装libboost1.71,到底安装了哪些东西。(最好看能不能找见包的构建过程,因为搜到一个比较有趣的东西boost-cmake)

    ➜ dpkg -L libboost1.71-all-dev 
    /.
    /usr
    /usr/share
    /usr/share/doc
    /usr/share/doc/libboost1.71-all-dev
    /usr/share/doc/libboost1.71-all-dev/copyright
    /usr/share/doc/libboost1.71-all-dev/changelog.Debian.gz
    
    
    ➜  dpkg -L libboost1.71-dev:amd64
    /usr/include/boost/**
    # 下面这几行可能对解决问题,有用
    /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0
    /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake
    /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfigVersion.cmake
    /usr/lib/x86_64-linux-gnu/cmake/BoostDetectToolset-1.71.0.cmake
    /usr/lib/x86_64-linux-gnu/cmake/boost_headers-1.71.0
    /usr/lib/x86_64-linux-gnu/cmake/boost_headers-1.71.0/boost_headers-config-version.cmake
    /usr/lib/x86_64-linux-gnu/cmake/boost_headers-1.71.0/boost_headers-config.cmake
    /usr/share/doc/**
    /usr/share/lintian/**
    
    ➜  dpkg -L libboost1.71-tools-dev
    ....
    
    ➜  dpkg -L libboost-program-options1.71-dev:amd64 
    # 下面这几行可能对解决问题,有用
    /usr/lib/x86_64-linux-gnu/cmake
    /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0
    /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/boost_program_options-config-version.cmake
    /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/boost_program_options-config.cmake
    /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant-shared.cmake
    /usr/lib/x86_64-linux-gnu/cmake/boost_program_options-1.71.0/libboost_program_options-variant-static.cmake
    /usr/lib/x86_64-linux-gnu/libboost_program_options.a
    
    ....
    ....
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    问题解决

  • 相关阅读:
    工业路由器在工厂数字化的应用及价值
    求后缀表达式的值
    npm命令大全
    模拟实现库函数,strtsr,memmove.
    企业有了BI,为什么还需要以指标为核心的ABI平台?
    AcWing 4520:质数 ← BFS
    spring缓存注解@Cacheable和@CacheEvict,设置过期时间和批量模糊删除
    微信开发之一键踢出群聊的技术实现
    Debezium系列之:优化Debezium使用方式,更新Debezium connector配置,不重启Debezium connector
    C++ 重载运算符和重载函数
  • 原文地址:https://blog.csdn.net/sinat_38816924/article/details/125024145