Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在linux上将boost静态链接到共享objet时出现问题_C++_Linux_Boost - Fatal编程技术网

C++ 在linux上将boost静态链接到共享objet时出现问题

C++ 在linux上将boost静态链接到共享objet时出现问题,c++,linux,boost,C++,Linux,Boost,我使用的是Boost 1.57.0 我用下面的命令编译了它 sudo ./b2 install --prefix=/home/sumkin/boost_build cxxflags='-fPIC' link=static 当我编译共享库到我想要静态链接boost的地方时,我得到了以下错误 /usr/bin/ld: /home/sumkin/boost_build/lib/libboost_filesystem.a(operations.o): relocation R_X86_64_32 a

我使用的是Boost 1.57.0

我用下面的命令编译了它

sudo ./b2 install --prefix=/home/sumkin/boost_build cxxflags='-fPIC' link=static
当我编译共享库到我想要静态链接boost的地方时,我得到了以下错误

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

这似乎有效。

您是否使用-fPIC选项编译共享库?您可以在上面看到cxxflags='-fPIC',是的,但我看到它们用于编译Boost库,而不是您在“编译共享库时”中提到的共享库你能补充一些更好的解释吗?不。我可以向你展示我们用来构建静态boost库的脚本,并告诉你它可以在这些库中构建共享libs链接。
CXX_COMPILER_FLAGS=-std=gnu++14 -fPIC -g-ggdb3 -O0 -gdwarf-3