[C++-sig] Boost python and import error
Valentino Volonghi
dialtone at gmail.com
Sun Mar 11 15:03:09 CET 2007
hi all, I'm trying to use boost to write python extensions in C++ under OSX.
I'm trying to use the most basic example of a greet function. The
extension module
is inside a reactor package and it's called _reactor.
Unfortunately after a compilation (that successfully finishes) I get
the following error:
>>> from reactor import _reactor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "reactor/__init__.py", line 11, in <module>
from reactor import _reactor
ImportError: dynamic module does not define init function (init_reactor)
I've searched for this on google and found many answers but none
worked. Actually
after looking at boost/python.hpp source I think that init_reactor
should be there
since it's inserted by BOOST_PYTHON_MODULE macro. Do I really have to use
boost.jam to compile this small example? (I ask because I tried hard
to use it under
osx last week and failed miserably, I simply can't use it and
documentation is lacking at best).
Thanks for information!
Here is the full source:
#include <boost/python.hpp>
using namespace boost::python;
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(_reactor)
{
def("greet", greet);
}
and here's the setup.py:
#!/usr/bin/python
import sys, os
from os.path import join
from distutils.core import setup
from distutils.extension import Extension
from distutils import sysconfig
# break abstraction to set g++ as linker - is there better way?
sysconfig._init_posix()
compiler = "g++ -ftemplate-depth-50"
#compiler += " -fkeep-inline-functions" # for debugging
# workaround for bugs in Redhat 7.3 compiler
if os.popen("g++ --version").read().strip() == "2.96":
compiler += " -fno-inline "
sysconfig._config_vars["CC"] = compiler
if sys.platform == 'darwin':
sysconfig._config_vars["LDSHARED"] = "g++ -undefined dynamic_lookup"
else:
sysconfig._config_vars["LDSHARED"] = "g++ -shared"
ext_modules = []
include_dirs = ["./include", "/usr/local/include", "/opt/local/include/"]
if os.environ.has_key("BOOST_INCLUDE"):
include_dirs.append(os.environ["BOOST_INCLUDE"])
libraries = []
for libpath in (os.environ.get("LD_LIBRARY_PATH", "").split(":") +
["/usr/lib", "/lib", "/usr/local/lib", "/opt/local/lib/"]):
paths = [os.path.join(libpath, libname) for libname in
("libboost_python.so", "libboost_python.dylib")]
for path in paths:
if os.path.exists(path):
libraries.append("boost_python")
break
if not "boost_python" in libraries:
libraries.append("boost_python-gcc-mt")
ext_modules.append(Extension("reactor._reactor", [join("reactor",
"%s.cpp" % name) for name in
("_reactor",)],
libraries=libraries,
library_dirs=["/usr/local/lib", "/opt/local/lib"],
include_dirs=include_dirs))
setup(
name = "reactor",
packages=["reactor"],
ext_modules=ext_modules,
)
--
Valentino Volonghi aka Dialtone
Now running MacOS X 10.4
TechBlog: http://vvolonghi.blogspot.com
Home Page: http://www.twisted.it
More information about the Cplusplus-sig
mailing list