Jump to content
  • Advertisement
Sign in to follow this  
perfectly_dark

Boost::python help

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to use boost::python in a project but it's seems to be quite difficult. And the lack of detailed documentation isn't helpful, I spent last night figuring out that I have to compile Boost::python first. Anyways, now it seems to be compiling some functions I'm exposing to python. But I don't want to make a python module or bother with bjam. All I want is to embed the python interpreter in my app and use scripts to call C++ functions. Is this even possible? I'm working with this: http://www.boost.org/libs/python/doc/index.html but it doesn't seem to be covered. Does anyone have any source code that does this? Here's what I've got but it doesn't actually do anything (probably cause I'm an idiot :P):
#include <iostream>
#include <boost/python.hpp>
#include <python.h>

using namespace boost::python;

char const * hello()
{
	return "hello dude";
}

BOOST_PYTHON_MODULE(test)
{
	def("hello", hello);
}

int main()
{
	Py_Initialize();
		PyObject * pName = PyString_FromString("test");
		PyObject * pModule = PyImport_Import( pName );
		if(! pModule )
		{
			std::cout << "Failed to load script" << std::endl;
		}
	Py_Finalize();

	return 0;
}
and test.py:
import test
test.hello()
Any help or insight would be greatly appreciated. And sorry if anything I ask is stupid, this is my first foray into scripting. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
This is probably a good sign that I need to finish my article on embedding Python. Anyways, in order to actually expose your module to the Python interpreter you need to call the init function. boost::python calls it init(your_module_name), so in your case it looks like inittest(). Just call it after your Py_Initialize() call. (I give my Python modules capitalized names so it doesn't look so funky, like module Test would become initTest()). I would also test the module first with PyRun_String() before you try loading a script from the disk.

Oh and calling the module that you expose the same name as the script you are trying to load is probably not a happy thing either.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!