It's necessary to call Py_Initialize() to initialize the interpreter before the use of any python object:
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
#include <iostream>
#include <boost/python/tuple.hpp>
#include <boost/python/dict.hpp>
//using namespace std;
using namespace boost::python;
int main()
{
Py_Initialize();
dict x;
x["123"] = 3;
return 0;
}
Boost Python offers a lot of functionalities to interface C++ with Python, but also some funcionalities to create C extensions in a higher level using C++. The code above is perfectly correspondent to the code bellow:
#include <Python.h>
#include <dictobject.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PyObject *d = PyDict_New();
PyDict_SetItemString(d, "123", PyLong_FromLong(3));
return 0;
}
Inside PyDict_SetItemString
there is a call to PyUnicode_InternInPlace that basically tries to use a string that already exists, otherwise it creates a new one (remember that python strings are immutable).
The segfault (when Py_Initilize
is not called) occurs inside this function, because Python needs to query it's runtime environment to check for the string, but once the envirnment is not loaded, it crashes.
It's not necessary to explicit call Py_Initilize
when creating a .dll, because it is imported inside the interpreter that already called it during its initialization.
.dll
, I don't usePy_Initialize()
, it works well. Could you tell me the details? – Limnology