首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在linux下针对库链接时Cython生成问题

在linux下针对库链接时Cython生成问题
EN

Stack Overflow用户
提问于 2013-08-01 11:38:19
回答 1查看 1.9K关注 0票数 1

我使用cython为C++项目提供。为此,我将C++项目构建为一个静态库,并在Cythonsetup.py脚本中对其进行链接。这在OSX下运行得很好,但是在Linux下我得到了以下错误:

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

这是我的setup.py

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

(将-fPIC添加到extra_link_argsextra_compile_args中,无论这意味着什么,都没有帮助)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-01 12:00:32

问题是,如果没有使用-fPIC构建任何静态依赖项,GCC就无法静态链接共享库。在这里,NetworKit是在没有-fPIC的情况下构建的,所以您要么需要使用-fPIC重新构建静态NetworKit-Core-0,要么需要动态链接它。

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

https://stackoverflow.com/questions/17993003

复制
相关文章

相似问题

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