[C++-sig] boost::python on Linux

Stefan Seefeld seefeld at sympatico.ca
Tue Jun 23 21:06:00 CEST 2009


On 06/23/2009 02:45 PM, Jim Treadway wrote:
> I'm having trouble getting a simple boost::python sample program to
> work properly on Linux.  It compiles and seems to link properly, but the Python
> interpreter is unable to call my C++ function.  On Mac OS X the
> program works as expected.
>
> Any help would be appreciated, hopefully I'm missing something obvious.
>    


Let's see:

I expect the problem to be that the Python interpreter doesn't 'see' the 
'Test' module. While normally this would just result in an import error, 
here it may just happen  to find a different 'Test' module, which, as it 
doesn't match what you expect, raises an ArgumentError.


> -- BEGIN --
> #include<stdlib.h>
> #include<string>
> #include<boost/python.hpp>
>
> using namespace boost::python;
>
> /* g++ -I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o
> test test.cpp */
>
> int test(int i)
> {
>         fprintf(stderr, "%s:\n", __FUNCTION__);
>         return i * 5;
> }
>
> BOOST_PYTHON_MODULE(Test)
> {
>         using namespace boost::python;
>         def("test", test);
> }
>
> int main(int argc, char *argv[])
> {
>         Py_Initialize();
>
>         try {
>                 initTest();
>
>    


I'm actually not sure what the effect of calling 'initTest()' is.
I believe you should call:

   PyImport_AppendInittab(const_cast<char*>("Test"), initTest);

>                 PyRun_SimpleString("import Test");
>                 PyRun_SimpleString("print 'calling test function'");
>                 PyRun_SimpleString("print Test.test(5)");
>         } catch (boost::python::error_already_set) {
>                 PyErr_Print();
>         }
>
>         Py_Finalize();
>         return 0;
> }
> -- END --
>    

HTH,
         Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list