[[email protected] python]$ cat hello_world.cc
#include <string>
#include <Python.h>
#include <boost/python.hpp>
namespace {
std::string greet() { return "Helloworld"; }
}
using namespace boost::python;
BOOST_PYTHON_MODULE(hello_world)
{
def("greet",greet);
}
[[email protected] python]$ g++ -c -fPIC hello_world.cc -I/path/to/boost/headers -I/path/to/python/headers -o hello_world.o
[[email protected] python]$ g++ -shared -Wl,-soname,libhello_world.so -o libhello_world.so hello_world.o
[[email protected] python]$ python
Python 2.7.1 (r271:86832, Jan 10 2011, 09:46:57)
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('.')
>>> import hello_world
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named hello_world
>>>
我創建瞭如上所示的.so文件,但我無法在python中導入。我錯過了什麼?如何從.so文件導入python模塊?
感謝。現在我得到'ImportError:./hello_world.so:undefined symbol:_ZNK12boost_1_47_06python7objects21py_function_impl_base9max_arityEv' – balki
@balki:你沒有鏈接到Boost.Python。 –
我鏈接到boost_python,現在我得到'ImportError:libboost_python:無法打開共享對象文件:沒有這樣的文件或目錄。如果我導出'LD_LIBRARY_PATH =/path/to/boost_python_lib',它工作正常。我如何在cmdline中指定? – balki