【发布时间】:2017-03-24 00:47:22
【问题描述】:
我想为我的共享库创建一个 python 扩展。我可以使用 distutils 构建和安装它。但是,当我导入模块时,出现“未定义符号”错误。
假设我的共享库“libhello.so”包含一个函数。
#include <stdio.h>
void hello(void) {
printf("Hello world\n");
}
g++ -fPIC hello.c -shared -o libhello.so
$ file libhello.so
libhello.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped
这是我的 setup.py
#!/usr/bin/env python
from distutils.core import setup, Extension
vendor_lib = '/home/sanjeev/trial1/vendor'
module1 = Extension("hello",
sources = ["hellomodule.c"],
include_dirs = [vendor_lib],
library_dirs = [vendor_lib],
runtime_library_dirs = [vendor_lib],
libraries = ['hello'])
setup(name = 'foo',
version = '1.0',
description = 'trying to link extern lib',
ext_modules = [module1])
在运行设置中
$ python setup.py install --home install
$ cd install/lib/python
$ python
Python 2.7.2 (default, Aug 5 2011, 13:36:11)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.architecture()
('64bit', 'ELF')
>>> import hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./hello.so: undefined symbol: hello
【问题讨论】:
标签: python-2.7 python-c-extension