Cython在linux下链接到库时会产生问题
Cython build problems when linking against library under linux
我使用cython为C++项目提供Python包装器。为此,我将C++项目构建为一个静态库,并在Cythonsetup.py
脚本中与之链接。这在OSX下运行良好,但在Linux下我会得到以下错误:
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
:
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)
(不管这意味着什么,在extra_link_args
和extra_compile_args
中添加-fPIC
都没有帮助)
问题是,如果任何静态依赖项不是用-fPIC
构建的,GCC就无法静态链接共享库。在这里,NetworKit是在没有-fPIC
的情况下构建的,因此您需要使用-fPIC
重建静态NetworKit-Core-0,或者动态链接它。
相关文章:
- 如何在 Mac 上正确编译C++,当它在 Linux 上编译没有问题时?
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 我在使用 boost::serialization 时遇到了一个问题,我的代码在 Linux 中运行良好,但在 Wind
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- 编译问题与 Ubuntu Linux
- 获取处理器相关性(linux)时出现问题
- C++ Linux 上的插件 ABI 问题
- Linux 测量时间问题! std::chrono, QueryPerformanceCounter, clock_ge
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 为什么我的C 代码在Linux上运行,而在Windows上不运行,是否有RAM内存的问题
- Cmake Mac OSX库链接问题:在Linux上进行编译,但在Mac上进行了编译
- Vulkan + GLFW + Cmake在Linux环境下的链接问题
- Visual Studio 2017问题编译了C 代码,而该代码则在Linux中编译
- 在 Linux 中从单独的单线程进程生成多线程进程时出现问题
- 尝试模拟Linux shell.使用模拟MKDIR的问题
- 在程序中获取 Linux 'dd' 命令的输出时遇到问题C++
- Linux 中 gcc 4.3 版本的编译问题
- 调试基于 Linux 的 C++ 代码 Visual Studio 代码时出现问题
- VS2017 Linux C 线程问题
- 克服下载CSV文件进行分析时的问题.Linux与Windows新品Charachter