[C++-sig] importing embedded dynamic lib

Graeme Lufkin gwl at u.washington.edu
Wed Jun 9 18:32:47 CEST 2004


	I've done this myself; let me try and explain what I did.  First, write
the extension module as normal.  I used Boost.Python, but what's
important is having access to the 'extern "C" void
init<modulenamehere>()' function pointer.  
Second, instead of compiling your module into its own shared library
(.so file), simply link it in with your executable.  Using the
Boost.Build system, you can just add the .cpp files that make up your
module to the dependency list of the executable.
Third, before you create the embedded interpreter, make a call to
PyImport_AppendInittab().  For example, if my module is called funcs,
the init function is declared as 'extern "C" void initfuncs();', so I
call:
PyImport_AppendInittab("funcs", initfuncs);
then I call Py_Initialize().
Fourth, finally I can actually import the module into my embedded
interpreter:
PyRun_SimpleString("import funcs");

	So, if you follow my steps, you have only one binary file, the
executable.  This file contains code for the module, as well as the
regular C++ stuff of your program.  You embed the Python interpreter,
and import your module into its Python namespace.
	The embedding part of the Boost.Python tutorial is a bit lacking, and
some of the code snippets won't compile, but give it a try anyway, as
there's no other option ;)
	Hope this helps

> I have a basic dynamic lib "noddy.so" which I want to
> 1- embed inside my executable
> 2- import by embedded python interpreter using an API call.
> 
> I have two questions:
> 
> 1- what would be the recommended technique to embed a binary file
> inside 
> an executable?
> My guess would be to add the binary "noddy.so" to the linking command,
> but then
> how do I get access to that chunk of code in my source file? My goal 
> would be to achieve something like
> 
>     int* noddy_so = <binary here> ... but then how do I include the 
> binary code, or make the source code aware of it?
> 
> Using "#include" doesn't feel right, as this is a binary file, not 
> ASCII. And loading the file at run-time isn't right either, I need to 
> load it at compile time. And is "int*" the appropriate data type for 
> binary data?
> 
> 2- Let's imagine that I managed to load "noddy.so" into my source code
> somehow, and now have a pointer to it.
> Any hints as to the  correct python API call to import a dynamic lib 
> from a memory buffer? Could someone
> tell me for example if the following makes sense for my purpose?
> 
> ------------------------------
> char *modulename="noddy", *filename="no_real_file";
> PyObject *tmp;
> 
> /*Here try to translate the binary lib into a PyObject*/
> tmp = Py_CompileString(noddy_so, filename, Py_file_input);
> if (tmp == NULL)
>       error("Can't compile module [tmp]");
> 
> /*Import the "noddy" module*/
> pmod = PyImport_ExecCodeModule(modulename, tmp);
> if (pmod == NULL)
>       error("Can't load module [pmod]");
> ------------------------------
> 
> Regards,
> Vio
-- 
	- Graeme
	gwl at u.washington.edu
"This sentence contains exactly threee erors."





More information about the Cplusplus-sig mailing list