[C++-sig] Re: compile errors with embedding example from tutorial

Faheem Mitha faheem at email.unc.edu
Fri Jul 2 00:48:24 CEST 2004


On Thu, 1 Jul 2004 13:52:17 -0700, Graeme Lufkin
<graeme.lufkin at gmail.com> wrote:

> On Thu, 1 Jul 2004 20:19:37 +0000 (UTC), Faheem Mitha
> <faheem at email.unc.edu> wrote:
>>  
>> >     I've had similar problems, and have slogged my way to figuring
>> > them out.  The tutorial is wrong, at least on gcc 3.2.3.
>> >   Here's what you want:  After calling Py_Initialize(), you should get
>> > the Python main namespace object by doing:
>> >
>> > handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
>> > dict main_namespace = extract<dict>(PyModule_GetDict(main_module.get()));
>> 
>> I think this should be main_module.ptr() since main_module is an object.
>
>    In my example it's a handle<>, so get() is correct.  In Dave's (and
> your) examples, it's an object, so ptr() is correct.

Ok. Sorry. I stand corrected.

>> I'm trying with
>> 
>> ******************************************************************************
>> #include <boost/python.hpp>
>> using namespace boost::python;
>> 
>> void foo()
>> {
>>   Py_Initialize();
>>   
>>   object main_module =
>> object(handle<>(borrowed(PyImport_AddModule("__main__"))));
>> 
>>   dict main_namespace = extract<dict>(PyModule_GetDict(main_module.ptr()));
>> 
>>   handle<> unused(PyRun_String("hello = file('hello.txt', 'w')\n"
>>                           "hello.write('Hello world!')\n"
>>                           "hello.close()", Py_file_input,
>>                           main_namespace.ptr(),main_namespace.ptr()));
>>     Py_Finalize();
>> }
>> 
>> BOOST_PYTHON_MODULE(embed)
>> {
>>   def("foo",foo);
>> }
>> *******************************************************************************
>> 
>> This compiles, but I get the same error as before, namely
>> 
>> NameError: name 'file' is not defined
>> 
>> Does this work correctly for you? I'm wondering if there is some issue
>> with my compiler flags. I'm trying to get Boost.Build to work but
>> there are some linking problems.
>
>   First, your extension module should never call Py_Initialize() or
> Py_Finalize(), as Python will already be initialized by the time
> you're running methods in it.  I think what you want is foo() to be
> main(), with no extension module stuff needed.  Also, Boost.Python and
> Py_Finalize() don't play nice, so never call it (check the list
> archive for details, I don't really understand why myself).

>   Second, yes this compiles and runs correctly for me, after removing
> the Py_Initialize and Py_Finalize calls.

Just to make sure we are on the same page here. I want to run the
interpreter inside an extension module. In other words, I want an
extension module called embed.so, not an executable called embed.

>   Third, you definitely need to use Boost.Build for a few sample
> programs before you think about switching back to Makefiles.  I share
> your frustration at Boost.Build sometimes, but it does a particular
> job admirably.  I submitted some patches to the docs and the Jamfile
> rules a while ago that were aimed at making it easier to use
> Boost.Build for extensions and embedded apps.  Dave hasn't gotten back
> to me yet. (Hint hint)

Up to now I had been using distutils for extension modules, which has
worked fine for examples which don't embed the interpreter. It doesn't
seem to be working for this example, though.

I've been trying to use Boost.Build for this example. I added a
section to libs/python/example/Jamfile as follows. 

*******************************************************************
# ----- embed -------

# Declare a Python extension called embed
extension embed
:  # sources
        embed.cc

  # requirements and dependencies for Boost.Python extensions
  <template>@boost/libs/python/build/extension
  ;
*******************************************************************

This builds fine, but when I try to load it, I get

In [1]: import embed
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call
last)

/usr/local/src/libboost-python1.31.0/boost-1.31.0/libs/
python/example/bin/example/embed.so/gcc/debug/shared-linkable-true/<console>

ImportError: libboost_python.so.1.31.0: cannot open shared object
file: No such file or directory

Any idea how to fix this? Debian has /usr/lib/libboost_python.so and
libboost_python-gcc-mt-1_31.so, where the former is a symbolic link to
the latter.

There seems to be some gcc.mak files floating around, and it looks
like I could modify the shared libs line appropriately, but I am not
sure which file I need to modify.

In any case, could you tell me what the compile lines you are using
for the example above are? I could try it and see.

                                                              Faheem.





More information about the Cplusplus-sig mailing list