[C++-sig] Embedding Tutorial

David Abrahams dave at boost-consulting.com
Thu Dec 19 00:54:07 CET 2002


"William Trenker" <wtrenker at hotmail.com> writes:

> I've just been studying the embedding tutorial and wondering if it is
> really necessary to use BOOST_PYTHON_MODULE() in all cases.  Is what
> follows feasible?
>
> #include <boost/python.cpp>
> #include <stdio.h>
> using namespace boost::python;
> int main(int argc, char** argv): {
>     Py_Initialize();
>     handle<> main_module( borrowed(PyImport_AddModule("__main__")) );
>     handle<> main_namespace( borrowed(PyModule_GetDict(main_module)) );
>     handle<> result( PyRun_String("1 + 1",
>             Py_eval_input, main_namespace.get(), main_namespace.get()) );
>     object py_result(result);
>     int result = extract<int>(py_result);
>     printf("result = %i\n",result);
>     //Py_Finalize();  AVOID (for now)
>     return 0;
> };

Not being an embedding expert, I can't tell you for sure.

> (The above may have errors; I haven't been able to test the code.  I
> don't yet know how to make a Jamfile that builds a
> boost.python-enbled executable rather than an extension module.  So
> please consider this as psuedocode for now.)

That part I can help you with.  This will build and run an executable

-- SCHNIPP --
subproject libs/python/user ;

# bring in the rules for python
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;

# bring in rules for testing
SEARCH on testing.jam = $(BOOST_BUILD_PATH) ;
include testing.jam ;

run source-file1.cpp source-file2.cpp <dll>../build/boost_python
  : # program args
  : # input files
  : # requirements
  $(PYTHON_PROPERTIES)
    <library-path>$(PYTHON_LIB_PATH)
    <find-library>$(PYTHON_EMBEDDED_LIBRARY) 
  : exe-name
;
-- SCHNAPP --

And this one just builds the executable; testing is your problem:

-- SCHNIPP --
subproject libs/python/user ;

# bring in the rules for python
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;

exe exe-name : source-file1.cpp source-file2.cpp <dll>../build/boost_python
  : # requirements
  $(PYTHON_PROPERTIES)
    <library-path>$(PYTHON_LIB_PATH)
    <find-library>$(PYTHON_EMBEDDED_LIBRARY) 
;
-- SCHNAPP --

HTH,
-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list