1
我要創建我的共享庫Python擴展。我能夠使用distutils來構建和安裝它。然而,當我導入模塊時得到「未定義的符號」錯誤。Python擴展不加載我的共享庫
說我的共享庫「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
作品。是的,這是問題所在。 –