首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接静态库与共享库问题

链接静态库与共享库问题
EN

Stack Overflow用户
提问于 2018-11-16 12:58:13
回答 1查看 733关注 0票数 0

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

我共享了跨平台库项目,它使用第三方静态库(Libtorrent).

Windows/Android/macOS -构建并运行良好。

但我不知道如何在Linux (Ubuntu)中用GCC构建它。

我得到以下链接器错误:

代码语言:javascript
复制
/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

我尝试过用BoostLibtorrent标记重新构建-fPIC:什么都没有改变。

Boost构建脚本:

代码语言:javascript
复制
#!/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构建脚本:

代码语言:javascript
复制
#!/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-based库。我使用Qt Creator构建它,在.pro文件中提供了所有必需的头和库。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-16 13:39:31

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

代码语言:javascript
复制
export CFLAGS="-fPIC"

重建,现在它链接没有错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53338407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档