[[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 파일을 만들었지 만 파이썬 안에서 가져올 수 없습니다. 나는 무엇을 놓치고 있습니까?.so 파일에서 파이썬 모듈을 가져 오는 방법은 무엇입니까?
감사 :
지금 당신은이 그래서 hello_world를 가져올 수 있습니다. 이제 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'을 export하면 잘 동작합니다. cmdline에서 어떻게 지정합니까? – balki