boost多版本安装记录

由于许多项目代码使用到了boost库,鉴于原始环境的boost库常常发生找不到模块的情况,所以进行了如下操作:

  1. 卸载原始版本boost
  2. 安装最新版本1.69boost
  3. 发现boost版本与电脑上cmake版本不对应,因此卸载1.69
  4. 安装1.58版本的boost

本机环境:ubuntu16.04

卸载boost

卸载boost的操作根据安装情况的不同,操作不一:
A. 对于使用apt-get安装的boost版本

sudo apt-get install libboost1.xx-dev # libboost1.66-dev for example 安装
dpkg -S /usr/include/boost/version.hpp # 查看安装的boost版本
sudo apt-get autoremove libboost1.66-dev # 卸载

但是对于使用源码编译的boost库,包括查看boost版本在内的所有上述代码不可用

B.使用源码编译的boost库,查看安装的boost版本如下

cat /usr/local/include/boost/version.hpp | grep “BOOST_LIB_VERSION”
#查看安装的boost版本. version.hpp 前面的路径可变,即找到/usr/路径下的version.hpp即可

卸载源码编译的boost库,需要删除:
1.安装路径的boost; 2. /usr/local/include/路径下的boost*; 3. /usr/local/lib/路径下的libboost*

sudo rm -r -f /usr/local/include/boost*
sudo rm -f /usr/local/lib/libboost*

2、3步骤需要使用sudo权限

ref: https://askubuntu.com/questions/325504/ubuntu-12-04-uninstall-boost-installed-from-source

CMake与boost的版本对应

查看本机cmake版本:

user@user-LabPc:~$ cmake -version
(output) cmake version 3.5.1
CMake与boost对应如下:

Boost 1.63 requires CMake 3.7 or newer.
Boost 1.64 requires CMake 3.8 or newer.
Boost 1.65 and 1.65.1 require CMake 3.9.3 or newer.
Boost 1.66 requires CMake 3.11 or newer.
Boost 1.67 requires CMake 3.12 or newer.
Boost 1.68, 1.69 require CMake 3.13 or newer.

所以根据本机的cmake情况安装对应的boost。
本机cmake版本为3.5,目测只能Boost5.x版本,因此选择Boost5.8源码完整版安装

ref: https://stackoverflow.com/questions/42123509/cmake-finds-boost-but-the-imported-targets-not-available-for-boost-version

boost源码安装

由于使用apt-get常常安装库不够完整,常常发生boost库找不到的问题:

/usr/bin/ld: 找不到 -lBoost::system
/usr/bin/ld: 找不到 -lBoost::filesystem
/usr/bin/ld: 找不到 -lBoost::thread

因此使用源码进行boost库完整版的编译安装。
安装步骤网上介绍比较多。

boost官网: https://www.boost.org/
历史boost版本:https://www.boost.org/users/history/
5.8版本:https://sourceforge.net/projects/boost/files/boost/1.58.0/
这里选择.tar.gz文件安装

sudo apt-get update && sudo apt-get install python-dev autotools-dev libicu-dev libbz2-dev # 安装boost依赖项
tar zxvf boost_1_58_0.tar.gz
cd boost_1_58_0/
./bootstrap.sh --with-libraries=all --with-toolset=gcc
./b2 toolset=gcc
./b2 install

几点注意:

第4、5步:

  • 在环境中有anaconda的情况时,会发生python路径找不到的情况,有针对Boost5.x和6…x有2种解决方案:
    首先找到缺失文件’pyconfig.h’文件所在路径,这里为/home/user/Software/Anaconda3/include/python3.6m
    第4步结束时,解压缩文件夹下会生成
    ‘project-config.jam’ (boost 5.8version,boost6.x版本也会有类似名字的config.jam文件产生)
    在该文件中改为如下:

if ! [ python.configured ]
{
using python : 3.6 : /home/user/Software/Anaconda3 : /home/user/Software/Anaconda3/include/python3.6m ;
}

上述操作对于Boost6.x有效而5.x无效。

  • 对于ver5.x需要在同一个terminal命令行输入

export CPLUS_INCLUDE_PATH=/home/user/Software/Anaconda3/include/python3.6m

若第5步成功则会出现:

The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
/home/user/Software/boost/boost_1_58_0
The following directory should be added to linker library paths:
/home/user/Software/boost/boost_1_58_0/stage/lib

这几个路径比较重要。

第6步的install可能需要sodu权限,因为是向/usr/路径下安装

至此,boost安装完成,可以使用简单的代码进行boost的测试:

#include <boost/thread/thread.hpp> //包含boost头文件
#include <iostream>
#include <cstdlib>
using namespace std;

volatile bool isRuning = true;

void func1()
{
    static int cnt1 = 0;
    while(isRuning)
    {
        cout << "func1:" << cnt1++ << endl;
        sleep(1);
    }
}

void func2()
{
    static int cnt2 = 0;
    while(isRuning)
    {
        cout << "\tfunc2:" << cnt2++ << endl;
        sleep(2);
    }
}

int main()
{
    boost::thread thread1(&func1);
    boost::thread thread2(&func2);

    system("read");
    isRuning = false;

    thread2.join();
    thread1.join();
    cout << "exit" << endl;
    return 0;
}

编译

g++ main.cpp -g -o main -lboost_thread

若报错,可以增加-lpthread

g++ main.cpp -g -o main -lboost_thread -lpthread

上述代码在Boost6.9是编译产生一个a.out文件,执行通过。
但是在ver5.8情况下,编译一直报错,查询了一下,貌似是ver5.x版本自己的问题,6.x不会出现该问题 =。=

/usr/bin/ld: /tmp/ccthjNEa.o: undefined reference to symbol ‘_ZN5boost6system15system_categoryEv’
//usr/local/lib/libboost_system.so.1.58.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

之后先没有管这个问题,直接上自己的项目测试,boost库成功找到并编译通过。所以没有深究,欢迎遇到同样问题的小伙伴留下你们的解决方法~

ref: https://blog.csdn.net/u011641865/article/details/73498533
https://github.com/boostorg/build/issues/289

Boost使用

后续使用中,还是会发生boost找不到的情况,猜测可能是boost安装不完全,所以无法找到boost全部的lib,所以需要使用boost的cmakeList.txt一般写法如下:

set(BOOST_FIND_MINIMUM_COMPONENTS serialization system filesystem thread program_options date_time timer chrono regex)
find_package(Boost REQUIRED COMPONENTS ${BOOST_FIND_MINIMUM_COMPONENTS})
include_directories(${Boost_INCLUDE_DIR})
发布了5 篇原创文章 · 获赞 6 · 访问量 6086

猜你喜欢

转载自blog.csdn.net/mmLxfz/article/details/88842712
今日推荐