link static library with shared library problem

816 views Asked by At

I'm new to Linux, so sorry if this my question is really stupid.

I have shared cross-platform library project, which uses third party static library (Libtorrent).

Windows/Android/macOS - builds and works fine.

But I can't figure out how to build it in Linux (Ubuntu) with GCC.

I'm getting the following linker errors:

/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

I've tried rebuilding Boost and Libtorrent with -fPIC flag: nothing is changed.

Boost build script:

#!/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 build script:

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

My shared lib itself is a Qt-based library. I build it using Qt Creator, providing all the required headers and libs in .pro file.

1

There are 1 answers

0
Alexander Dyagilev On BEST ANSWER

The problem was in Libtorrent build script. I removed -fPIE option and added

export CFLAGS="-fPIC"

Rebuilt and now it links without errors.