簡體   English   中英

鏈接靜態庫與共享庫問題

[英]link static library with shared library problem

我是Linux新手,如果我的問題真的很愚蠢,對不起。

我共享了一個跨平台的庫項目,該項目使用了第三方靜態庫( Libtorrent )。

Windows / Android / macOS可以正常運行。

但是我不知道如何用GCCLinuxUbuntu )上構建它。

我收到以下鏈接器錯誤:

/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

我嘗試使用-fPIC標志重建BoostLibtorrent :什么都沒有改變。

提升構建腳本:

#!/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文件中提供了所有必需的標頭和庫。

問題出在Libtorrent構建腳本中。 我刪除了-fPIE選項並添加了

export CFLAGS="-fPIC"

重建,現在它鏈接沒有錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM