链接静态库与共享库问题

时间:2018-11-16 12:58:14

标签: c++ linux gcc libtorrent libtorrent-rasterbar

我是Linux的新手,很抱歉,如果我的问题真的很愚蠢。

我有一个共享的跨平台库项目,该项目使用第三方静态库(Libtorrent)。

Windows / Android / macOS-可以正常运行。

但是我不知道如何使用LinuxUbuntuGCC)中构建它。

我收到以下链接器错误:

/usr/bin/ld: /home/user/Desktop/project/libtorrent-build/prebuilt/linux/lib/libtorrent-rasterbar.a(create_torrent.o): relocation R_X86_64_TPOFF32 against symbol `_ZN5boost4asio6detail15keyword_tss_ptrINS1_10call_stackINS1_15task_io_serviceENS1_27task_io_service_thread_infoEE7contextEE6value_E' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /home/user/Desktop/project/libtorrent-build/prebuilt/linux/lib/libtorrent-rasterbar.a(disk_io_thread.o): relocation R_X86_64_TPOFF32 against symbol `_ZN5boost4asio6detail15keyword_tss_ptrINS1_10call_stackINS1_15task_io_serviceENS1_27task_io_service_thread_infoEE7contextEE6value_E' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /home/user/Desktop/project/libtorrent-build/prebuilt/linux/lib/libtorrent-rasterbar.a(peer_connection.o): relocation R_X86_64_TPOFF32 against symbol `_ZN5boost4asio6detail15keyword_tss_ptrINS1_10call_stackINS1_15task_io_serviceENS1_27task_io_service_thread_infoEE7contextEE6value_E' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /home/user/Desktop/project/libtorrent-build/prebuilt/linux/lib/libtorrent-rasterbar.a(session.o): relocation R_X86_64_TPOFF32 against symbol `_ZN5boost4asio6detail15keyword_tss_ptrINS1_10call_stackINS1_15task_io_serviceENS1_27task_io_service_thread_infoEE7contextEE6value_E' can not be used when making a shared object; recompile with -fPIC

我尝试使用Boost标志重建Libtorrent-fPIC:什么都没有改变。

提升构建脚本:

#!/bin/bash
set -e

BOOST_VERSION=1.63.0

BOOST_VERSION_UNDERSCORES=${BOOST_VERSION//./_}

echo "Boost version $BOOST_VERSION"
echo "Downloading..."

BOOST_ARCH_NAME=boost_${BOOST_VERSION_UNDERSCORES}.tar.gz

curl -O -L https://dl.bintray.com/boostorg/release/$BOOST_VERSION/source/$BOOST_ARCH_NAME

echo "Extracting..."
tar -xvzf $BOOST_ARCH_NAME

BOOST_FOLDER_NAME=boost_${BOOST_VERSION_UNDERSCORES}

cd $BOOST_FOLDER_NAME

chmod +x bootstrap.sh
./bootstrap.sh

chmod +x b2
./b2 headers

./b2    \
    --layout=versioned \
    --with-thread \
    --with-date_time \
    --with-filesystem \
        --with-chrono \
        --with-random \
    toolset=gcc \
    threading=multi \
    link=static \
    runtime-link=shared \
    variant=release \
    threadapi=pthread \
        debug-symbols=off \
        warnings=off \
        warnings-as-errors=off \
        architecture=x86 \
        address-model=64 \
    --stagedir=stage/linux \
        cxxflags="-std=gnu++0x -fPIC" \
    stage

echo "Copying prebuilt..."

mkdir -p ../prebuilt/linux/lib
cp -rf ./stage/linux/lib ../prebuilt/linux/

cd ../

echo "Cleaning up..."

rm $BOOST_ARCH_NAME
rm -rf $BOOST_FOLDER_NAME

echo "Done."

Libtorrent构建脚本:

#!/bin/sh
set -e

MY_PWD=$PWD

LIBTORRENT_VERSION=1_1_8
LIBTORRENT_NAME=libtorrent-$LIBTORRENT_VERSION
export LIBTORRENT_FOLDER_NAME=libtorrent-$LIBTORRENT_NAME

curl -O -L "https://github.com/arvidn/libtorrent/archive/${LIBTORRENT_NAME}.tar.gz"

echo "Extracting..."
rm -rf ./$LIBTORRENT_FOLDER_NAME
tar xfz "${LIBTORRENT_NAME}.tar.gz"

echo "Building for linux..."

cd $LIBTORRENT_FOLDER_NAME

. ./autotool.sh

mkdir -p lib

if [ ! -d "./include/boost" ]; then
  ln -s $PWD/../../boost-build/prebuilt/include/boost $PWD/include/boost
  ln -s $PWD/../../boost-build/prebuilt/linux/lib/* $PWD/lib/
fi

if [ ! -d "./include/openssl" ]; then
  ln -s $PWD/../../openssl-build/prebuilt/include/openssl $PWD/include/openssl
  ln -s $PWD/../../openssl-build/prebuilt/linux/lib/* $PWD/lib/
fi

export CXX=g++
export CC=gcc
export CXXFLAGS="-std=gnu++0x -fPIC -fPIE"
export CPPFLAGS="-I"$PWD"/include"

./configure --enable-debug=no --with-boost-python=no --enable-logging=no \
--enable-tests=no --with-openssl=$PWD \
--with-boost=$PWD \
--enable-shared=no \
--enable-encryption

make -j4

mkdir -p ../prebuilt/linux/lib
cp -rf ./src/.libs/libtorrent-rasterbar.a ../prebuilt/linux/lib/

cd $MY_PWD

echo "cleaning up"

rm -rf $LIBTORRENT_FOLDER_NAME
rm -rf ${LIBTORRENT_NAME}.tar.gz

echo "done"

我的共享库本身是一个基于Qt的库。我使用Qt Creator构建它,并在.pro文件中提供了所有必需的标头和库。

1 个答案:

答案 0 :(得分:0)

问题出在Libtorrent构建脚本中。我删除了-fPIE选项并添加了

export CFLAGS="-fPIC"

经过重建,现在可以正确链接了。