将 boost 静态链接到 linux 上的共享对象时遇到问题

troubles linking boost statically to shared objet on linux

我正在使用 Boost 1.57.0

我是用下面的命令编译的

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

当我将共享库编译到我想要 link 静态提升的位置时,我收到以下错误

/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.

我们使用此脚本静态构建 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

这似乎有效。