clstaudt
clstaudt

Reputation: 22488

Cython build problems when linking against library under linux

I use cython to provide Python wrappers for a C++ project. For this purpose I build the C++ project as a static library and link against it in the Cython setup.py script. This works fine under OSX, but under Linux I get the following error:

staudt ~/workspace/NetworKit-CommunityDetection/cython $ python3 setup.py build_ext --inplace
source files: ['NetworKit.pyx']
running build_ext
skipping 'NetworKit.cpp' Cython extension (up-to-date)
building 'NetworKit' extension
g++ -DNDEBUG -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -fPIC -I/usr/include/python3.2mu -c NetworKit.cpp -o build/temp.linux-x86_64-3.2/NetworKit.o -fPIC -fopenmp -std=c++11 -DNOLOG4CXX -DNOGTEST

g++ -pthread -shared build/temp.linux-x86_64-3.2/NetworKit.o -L../ -L../Core-O/ -L/usr/lib64 -lNetworKit-Core-O -lpython3.2mu -o /amd.home/home/staudt/workspace/NetworKit-CommunityDetection/cython/NetworKit.cpython-32mu.so -fopenmp -std=c++11
/usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: ..//libNetworKit-Core-O.a(PubWebGenerator.o): relocation R_X86_64_32S against `_ZTVN9NetworKit15PubWebGeneratorE' can not be used when making a shared object; recompile with -fPIC
..//libNetworKit-Core-O.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
error: command 'g++' failed with exit status 1

This is my setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext

import os
import shutil

# try-catch block when shutil.which is not available
try:
    if (shutil.which("g++-4.8") is not None):
        os.environ["CC"] = "g++-4.8"
        os.environ["CXX"] = "g++-4.8"

    elif (shutil.which("g++-4.7") is not None):
        os.environ["CC"] = "g++-4.7"
        os.environ["CXX"] = "g++-4.7"

    else:
        print("Using: {0} and {1}".format(os.environ["CC"], os.environ["CXX"]))
except:
    os.environ["CC"] = "g++"
    os.environ["CXX"] = "g++"


srcDir = "../src"
src = ["NetworKit.pyx"] # list of source files

print("source files: {0}".format(src))

modules = [Extension("NetworKit",
                    src,
                    language = "c++",
                    extra_compile_args=["-fopenmp", "-std=c++11", "-DNOLOG4CXX", "-DNOGTEST"],
                    extra_link_args=["-fopenmp", "-std=c++11"],
                    libraries=["NetworKit-Core-O"],
                    library_dirs=["../", "../Core-O/"])]

for e in modules:
    e.cython_directives = {"embedsignature" : True}

setup(name="NetworKit",
     cmdclass={"build_ext": build_ext},
     ext_modules=modules)

(Adding -fPIC, whatever that means, to the extra_link_args and extra_compile_args did not help)

Upvotes: 2

Views: 1957

Answers (1)

bennofs
bennofs

Reputation: 11973

The problem is that GCC cannot statically link a shared library if any of the static dependencies weren't build with -fPIC. Here, NetworKit is build without -fPIC, so you either need to rebuild the static NetworKit-Core-0 using -fPIC or link it dynamically.

Upvotes: 3

Related Questions