麻烦地将boost静态链接到linux上的共享对象

时间:2015-03-17 22:10:02

标签: c++ linux boost

我正在使用Boost 1.57.0

我使用以下命令编译它

sudo ./b2 install --prefix=/home/sumkin/boost_build cxxflags='-fPIC' link=static

当我将共享库编译到我想要静态链接的地方时,我得到了以下错误

/usr/bin/ld: /home/sumkin/boost_build/lib/libboost_filesystem.a(operations.o): 
relocation R_X86_64_32 against `.rodata.str1.1' can not 
be used when making a shared object; recompile with -fPIC
/home/sumkin/boost_build/lib/libboost_filesystem.a: error adding    
symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [libhtkernel.so] Error 1
make[1]: *** [CMakeFiles/htkernel.dir/all] Error 2
make: *** [all] Error 2

我做错了什么?我在x86_64-linux-gnu。

1 个答案:

答案 0 :(得分:0)

我们使用此脚本静态构建boost,然后从中构建共享库:

#! /bin/bash
set -eux
. ${VOLUMEDRIVER_BUILD_CONFIGURATION?"You need to set the path to the build configuration file"}

BOOST_VERSION=boost_1_57_0
BOOST_DIR=$BOOST_VERSION
BOOST_PACKAGE=$BOOST_VERSION.tar.bz2
BOOST_URL=http://downloads.sourceforge.net/project/boost/boost/1.57.0/$BOOST_PACKAGE
BOOST_BUILD_DIR=boost_build

. ../definitions.sh
. ../../helper-functions.sh

get_package_or_die $BOOST_PACKAGE $BOOST_URL


echo ">+>+> Cleaning up from previous build"
rm -rf $BOOST_DIR $BOOST_BUILD_DIR
echo "<+<+< Done cleaning up from previous build"


echo ">+>+> Unpacking $BOOST_PACKAGE"
 tar -xjf $SOURCES_DIR/$BOOST_PACKAGE
echo "<+<+< Done unpacking $BOOST_PACKAGE"

echo ">+>+> Patching $BOOST_VERSION"
# patch -p0 < boost.patch0 # a more descriptive name would be nice

pushd $BOOST_DIR
patch -p1 < ../boost-make-valgrind-happy.patch
popd
 echo "<+<+< Done patching $BOOST_VERSION"

# Add the correct flags for boost compilation .. another build system..lovely
BOOST_COMPILER_FLAGS=""
for i in ${CXX_COMPILER_FLAGS}
do
BOOST_COMPILER_FLAGS="$BOOST_COMPILER_FLAGS <cxxflags>$i"
done

# Specifying which boost to use can be done here by checking:
# http://www.boost.org/doc/libs/1_53_0/libs/python/doc/building.html
# and specifying the correct using python line beneath. For now we just
# support the system python and trust that that is the one the boost
# build system will find.

cat > project-config.jam <<EOF
# Boost.Build Configuration
# Automatically generated by bootstrap.sh

import option ;
import feature ;

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
# if ! gcc in [ feature.values <toolset> ]
{
    using gcc : vd : $PREFIX/bin/vd-g++ : $BOOST_COMPILER_FLAGS ;
}

project : default-build <toolset>gcc ;

# Python configuration
using python : 2.7 : /usr ;

# List of --with-<library> and --without-<library>
# options. If left empty, all libraries will be built.
# Options specified on the command line completely
# override this variable.
libraries =  --without-math --without-wave --without-graph --without-graph_parallel --without-mpi ;

# These settings are equivivalent to corresponding command-line
# options.
option.set prefix : $PREFIX ;
option.set exec-prefix : $PREFIX ;
option.set libdir : $PREFIX/lib ;
option.set includedir : $PREFIX/include ;

# Stop on first error
option.set keep-going : false ;
EOF


echo ">+>+> Configuring $BOOST_VERSION"
pushd $BOOST_DIR
./bootstrap.sh --without-icu --without-libraries=math,wave,graph,graph_parallel,mpi --prefix=$PREFIX
echo "<+<+< Done configuring $BOOST_VERSION"

mv ../project-config.jam .

echo ">+>+> Building $BOOST_VERSION"
./b2 -a -d2 -q -j ${BUILD_NUM_PROCESSES-2} --build-dir=$BOOST_BUILD_DIR --disable-icu toolset=gcc-vd link=static variant=debug threading=multi
echo "<+<+< Done building $BOOST_VERSION"

echo ">+>+> Removing previous installation (if any)"
rm -rf ${PREFIX}/include/boost
rm -rf ${PREFIX}/lib/libboost*
echo "<+<+< Done removing previous installation"

echo ">+>+> Installing $BOOST_VERSION"
./b2 -a -d2 -q -j ${BUILD_NUM_PROCESSES-2} --build-dir=$BOOST_BUILD_DIR --disable-icu toolset=gcc-vd link=static variant=debug threading=multi install
echo "<+<+< Done installing $BOOST_VERSION"

BUILD_SIZE=`du -sh`
echo "Build used $BUILD_SIZE disk space"

echo ">+>+>+ Cleaning up"
popd
rm -rf $BOOST_DIR $BOOST_BUILD_DIR project-config.jam
echo "<+<+< Done cleaning up"
set -eux
. ${VOLUMEDRIVER_BUILD_CONFIGURATION?"You need to set the path to the build configuration file"}

BOOST_VERSION=boost_1_57_0
BOOST_DIR=$BOOST_VERSION
BOOST_PACKAGE=$BOOST_VERSION.tar.bz2
BOOST_URL=http://downloads.sourceforge.net/project/boost/boost/1.57.0/$BOOST_PACKAGE
BOOST_BUILD_DIR=boost_build

. ../definitions.sh
. ../../helper-functions.sh

get_package_or_die $BOOST_PACKAGE $BOOST_URL


echo ">+>+> Cleaning up from previous build"
rm -rf $BOOST_DIR $BOOST_BUILD_DIR
echo "<+<+< Done cleaning up from previous build"


echo ">+>+> Unpacking $BOOST_PACKAGE"
 tar -xjf $SOURCES_DIR/$BOOST_PACKAGE
echo "<+<+< Done unpacking $BOOST_PACKAGE"

echo ">+>+> Patching $BOOST_VERSION"
# patch -p0 < boost.patch0 # a more descriptive name would be nice

pushd $BOOST_DIR
patch -p1 < ../boost-make-valgrind-happy.patch
popd
 echo "<+<+< Done patching $BOOST_VERSION"

# Add the correct flags for boost compilation .. another build system..lovely
BOOST_COMPILER_FLAGS=""
for i in ${CXX_COMPILER_FLAGS}
do
BOOST_COMPILER_FLAGS="$BOOST_COMPILER_FLAGS <cxxflags>$i"
done

# Specifying which boost to use can be done here by checking:
# http://www.boost.org/doc/libs/1_53_0/libs/python/doc/building.html
# and specifying the correct using python line beneath. For now we just
# support the system python and trust that that is the one the boost
# build system will find.

cat > project-config.jam <<EOF
# Boost.Build Configuration
# Automatically generated by bootstrap.sh

import option ;
import feature ;

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
# if ! gcc in [ feature.values <toolset> ]
{
    using gcc : vd : $PREFIX/bin/vd-g++ : $BOOST_COMPILER_FLAGS ;
}

project : default-build <toolset>gcc ;

# Python configuration
using python : 2.7 : /usr ;

# List of --with-<library> and --without-<library>
# options. If left empty, all libraries will be built.
# Options specified on the command line completely
# override this variable.
libraries =  --without-math --without-wave --without-graph --without-graph_parallel --without-mpi ;

# These settings are equivivalent to corresponding command-line
# options.
option.set prefix : $PREFIX ;
option.set exec-prefix : $PREFIX ;
option.set libdir : $PREFIX/lib ;
option.set includedir : $PREFIX/include ;

# Stop on first error
option.set keep-going : false ;
EOF


echo ">+>+> Configuring $BOOST_VERSION"
pushd $BOOST_DIR
./bootstrap.sh --without-icu --without-libraries=math,wave,graph,graph_parallel,mpi --prefix=$PREFIX
echo "<+<+< Done configuring $BOOST_VERSION"

mv ../project-config.jam .

echo ">+>+> Building $BOOST_VERSION"
./b2 -a -d2 -q -j ${BUILD_NUM_PROCESSES-2} --build-dir=$BOOST_BUILD_DIR --disable-icu toolset=gcc-vd link=static variant=debug threading=multi
echo "<+<+< Done building $BOOST_VERSION"

echo ">+>+> Removing previous installation (if any)"
rm -rf ${PREFIX}/include/boost
rm -rf ${PREFIX}/lib/libboost*
echo "<+<+< Done removing previous installation"

echo ">+>+> Installing $BOOST_VERSION"
./b2 -a -d2 -q -j ${BUILD_NUM_PROCESSES-2} --build-dir=$BOOST_BUILD_DIR --disable-icu toolset=gcc-vd link=static variant=debug threading=multi install
echo "<+<+< Done installing $BOOST_VERSION"

BUILD_SIZE=`du -sh`
echo "Build used $BUILD_SIZE disk space"

echo ">+>+>+ Cleaning up"
popd
rm -rf $BOOST_DIR $BOOST_BUILD_DIR project-config.jam
echo "<+<+< Done cleaning up"

我们设置的编译器标志是:

CXX_COMPILER_FLAGS=-std=gnu++14 -fPIC -g-ggdb3 -O0 -gdwarf-3

这似乎有效。