前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux下boost编译安装全过程脚本塈bzip2编译安装全过程脚本

linux下boost编译安装全过程脚本塈bzip2编译安装全过程脚本

作者头像
10km
发布2018-01-03 14:42:37
2K0
发布2018-01-03 14:42:37
举报
文章被收录于专栏:10km的专栏10km的专栏

boost编译安装

build_boost.sh

代码语言:javascript
复制
#!/bin/bash
shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
# 上一条命令执行出错则中止脚本执行
exit_on_error(){
    if [ ! $? -eq 0 ]
    then
        echo "exit for error"
        exit -1
    fi
}
# 如果文件/文件夹存在则删除
remove_if_exist(){
    if [ $# -eq 1 ]
    then
        if [ -e $1 ]
        then
            rm $1 -fr
            if [ ! $? -eq 0 ]
            then
                exit -1
            fi
        fi
        return 0
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 清除指定文件夹的内容,如果文件夹不存在则创建空文件夹
clearn_folder(){
    if [ $# -eq 1 ]
    then
        if [ -d "$1" ]
        then 
            rm -fr $1/*
        else 
            mkdir -p "$1"
        fi
        exit_on_error
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 生成安装路径名后缀
install_suffix(){
    if [ $# -eq 1 ]
    then
        typeset -l os processor
        os=`uname -s`
        processor=`uname -p`
        echo $1_${os}_${processor}
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 如果文件存在且checksum与$2指定的md5相等则返回 1,否则返回0
# $1 待检查的文件路径
# $2 md5校验码
need_download(){
    if [ $# -eq 2 ]
    then
        if [ -f $1 ]; then
            echo "File already exists. Checking md5..."
            os=`uname -s`
            if [ "$os" = "Linux" ]; then
                checksum=`md5sum $1 | awk '{ print $1 }'`
            elif [ "$os" = "Darwin" ]; then
                checksum=`cat $1 | md5`
            fi
            if [ "$checksum" = "$2" ]; then
                echo "Checksum is correct. No need to download $1."
                return 1
            else
                echo "Checksum is incorrect. Need to download again $1"
            fi
        else
            return 0
        fi
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
###############下载boost
BOOST_VERSION="1.58.0"
BOOST_FOLDER="boost_${BOOST_VERSION//./_}"
BOOST_PACKAGE="$BOOST_FOLDER.tar.gz"
# 注意这里的md5是1.58.0版本tar.gz包的md5校验码,如果下载其他版本则该值要重新计算并更新,
# 计算md5方法参见need_download函数
if need_download $BOOST_PACKAGE "5a5d5614d9a07672e1ab2a250b5defc5"
then
    echo "(下载源码)downloading boost $BOOST_VERSION source"
    wget --no-check-certificate https://nchc.dl.sourceforge.net/project/boost/boost/$BOOST_VERSION/$BOOST_PACKAGE
    exit_on_error
fi
remove_if_exist $BOOST_FOLDER
tar zxvf $BOOST_PACKAGE
exit_on_error

###################编译boost
#安装路径
install_folder=$(dirname $(readlink -f $0))/release/$(install_suffix boost)
echo install_folder:$install_folder
bzip2_path=$(dirname $(readlink -f $0))/release/$(install_suffix bzip2)
pushd boost_1_58_0
# 指定bzip2位置,编译iostreams库时需要
# 如果不指定编译iostreams时会报错找不到:bzlib.h
export LIBRARY_PATH=$bzip2_path/lib:$LIBRARY_PATH
export CPLUS_INCLUDE_PATH=$bzip2_path/include:$CPLUS_INCLUDE_PATH
# 不编译python库
./bootstrap.sh --without-libraries=python
exit_on_error
./b2 --clean
# 删除原安装路径
remove_if_exist $INSTALL_FOLDER
# --prefix 指定安装位置
# --debug-configuration 编译时显示加载的配置信息
# -q 参数指示出错就停止编译
# link=static 只编译静态库
./b2 --prefix=$INSTALL_FOLDER -q --debug-configuration link=static install

popd

以上脚本完成boost源码下载编译安装全过程,注意,如果没有安装bzip2,则在编译过程中会报错

libs/iostreams/src/bzip2.cpp:20:56: fatal error: bzlib.h: No such file or directory

解决这个问题有很简单的办法,就是apt安装libbz2-dev

sudo apt-get install libbz2-dev

但因为项目需要,不能使用编译好的二进制代码 ,我得编译安装bzip2,所以先执行下面的脚本再执行 build_boost.sh,boost才能正常编译。

bzip2编译安装

下面的脚本完成bzip2下载编译安装全过程。 build_bzip2.sh

代码语言:javascript
复制
#!/bin/bash
shell_folder=$(cd "$(dirname "$0")";pwd)
pushd $shell_folder
# 上一条命令执行出错则中止脚本执行
exit_on_error(){
    if [ ! $? -eq 0 ]
    then
        echo "exit for error"
        exit -1
    fi
}
# 如果文件/文件夹存在则删除
remove_if_exist(){
    if [ $# -eq 1 ]
    then
        if [ -e $1 ]
        then
            rm $1 -fr
            if [ ! $? -eq 0 ]
            then
                exit -1
            fi
        fi
        return 0
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 清除指定文件夹的内容,如果文件夹不存在则创建空文件夹
clearn_folder(){
    if [ $# -eq 1 ]
    then
        if [ -d "$1" ]
        then 
            rm -fr $1/*
        else 
            mkdir -p "$1"
        fi
        exit_on_error
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 生成安装路径名后缀
install_suffix(){
    if [ $# -eq 1 ]
    then
        typeset -l os processor
        os=`uname -s`
        processor=`uname -p`
        echo $1_${os}_${processor}
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
# 如果文件存在且checksum与$2指定的md5相等则返回 1,否则返回0
# $1 待检查的文件路径
# $2 md5校验码
need_download(){
    if [ $# -eq 2 ]
    then
        if [ -f $1 ]; then
            echo "File already exists. Checking md5..."
            os=`uname -s`
            if [ "$os" = "Linux" ]; then
                checksum=`md5sum $1 | awk '{ print $1 }'`
            elif [ "$os" = "Darwin" ]; then
                checksum=`cat $1 | md5`
            fi
            if [ "$checksum" = "$2" ]; then
                echo "Checksum is correct. No need to download $1."
                return 1
            else
                echo "Checksum is incorrect. Need to download again $1"
            fi
        else
            return 0
        fi
    else 
        echo invalid argument:
        echo $*
        exit -1
    fi
}
###############下载bzip2
BZIP2_VERSION="1.0.6"
BZIP2_FOLDER="bzip2-$BZIP2_VERSION"
BZIP2_PACKAGE="$BZIP2_FOLDER.tar.gz"
if need_download $BZIP2_PACKAGE "00b516f4704d4a7cb50a1d97e6e8e15b"
then
    echo "(下载源码)downloading bzip2 $BZIP2_VERSION source"
    wget --no-check-certificate http://www.bzip.org/$BZIP2_VERSION/$BZIP2_PACKAGE
    exit_on_error
fi
remove_if_exist $BZIP2_FOLDER
tar zxvf $BZIP2_PACKAGE
exit_on_error
bzip2_makefile=$BZIP2_FOLDER/Makefile
# 使用sed编译器修改Makefile,在编译选项中增加-fPIC参数
# 判断CFLAGS中是否已经有-fPIC选项,如果没有就添加,没有则不修改
if [ -z "$(grep '^\s*CFLAGS\s*=' $bzip2_makefile | grep '\-fPIC')" ] 
then
    echo "add -fPIC to CFLAGS in $bzip2_makefile"
    sed -i -r 's/(^\s*CFLAGS\s*=)(.*$)/\1-fPIC \2/' $bzip2_makefile
    exit_on_error
else
    echo "found -fPIC in CFLAGS,no need modify $bzip2_makefile"
fi
###################编译bzip2
#安装路径
install_folder=$(dirname $(readlink -f $0))/release/$(install_suffix bzip2)
echo install_folder:$install_folder

pushd bzip2-1.0.6
clearn_folder $install_folder
make clean
exit_on_error
make -j8
make install PREFIX=$install_folder 
exit_on_error
popd

另外,在编译bzip2之前,要修改bzip2的Makefile.在CFLAGS定义中增加-fPIC选项,如下:

CFLAGS=-fPIC -Wall -Winline -O2 -g $(BIGFILES)

否则编译boost时会报错,在下载bzip2源码的脚本中有相关的代码用于自动在CFLAGS定义中增加-fPIC选项

上面两个脚本中有不少相同的函数,为方便维护,在实际工程中,我是把它合并放在一个库文件中的,本文为了让每个脚本都能独立运行,才特别将公用函数分别复制到每个脚本中。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年06月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • boost编译安装
  • bzip2编译安装
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档