[C++-sig] trouble initializing module using Boost

David Abrahams dave at boost-consulting.com
Sun Jun 4 00:27:44 CEST 2006


"J.D. Yamokoski" <yamokosk at ufl.edu> writes:

> I have run into a compile error which is probably very easy to
> fix. I am trying to call PyImport_AppendInittab() to add the
> initialization of a module I have created with Boost. The exact
> details are of the call are:
>
> // Register the extension module
> if ( PyImport_AppendInittab("shapes", initshapes) == -1 ) {
> 		throw python_exception( "Failed to add shapes to the 	interpreter's 
> builtin modules" );
> }
>
> // Initialize the Python interpreter
> Py_Initialize();
> ....
>
> I believe the module is defined correctly - I have used the module in 
> the Python command line, just now trying to use it through the C-API. 
> Here is the definition:
>
> ---- BoxModule.h ----
> #include <boost/python.hpp>
> #include "Box.h"
> using namespace boost::python;
>
> ---- BoxModule.cpp ----
> #include "BoxModule.h"
>
> BOOST_PYTHON_MODULE(shapes)
> {
>     class_<Box>("MyBox")
> 	.add_property("height", &Box::getHeight, &Box::setHeight)
> 	.add_property("width",	&Box::getWidth,	&Box::setWidth)
> 	.add_property("length", &Box::getLength, &Box::setLength)
> 	.add_property("volume", &Box::getVolume, &Box::setVolume);
> }
>
> And finally, the compile error message is:
>
> error C2065: 'initshapes' : undeclared identifier
>
> So is initshapes put in a namespace? Or how do I pass this function to 
> PyImport_AppendInittab()?

You have to make a declaration visible before you can use it.  Unless
your PyImport_AppendInittab is in BoxModule.cpp after
BOOST_PYTHON_MODULE(shapes), you have to write

   extern "C" initshapes();

somewhere.
        
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list