I'm trying to develop a python extension in C++. I'd be building the module with boost.python.
I've compiled boost1.69.0
with clang
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
My class in C++ has member objects which are pointers to other classes, boost::unordered_map
and structures with pointer members as well. The class also has templated functions, functions with signature static void *(void * )
. In one of the header files I declare extern const
objects which are also defined. The header looks like:
my_class.hpp:
#include <otherclass.hpp>
#include <boost/python.hpp>
class DP{
// Class objects and methods defined here
};
#ifdef __cplusplus
extern "C" {
#endif
DP * newcase(const char *dir, int lenpath);
#ifdef __cplusplus
}
#endif
my_class.cpp:
DP * newcase(const char *dir, int lenpath){
std::string path(dir);
path = path.substr(0, lenpath);
fs::path runDir(path);
return new DP(runDir);
}
BOOST_PYTHON_MODULE(dp_py)
{
using namespace boost::python;
class_<DP>("DP", init<const char *>());
def("newcase", &newcase, return_value_policy<manage_new_object>());
}
Compilation and linking is also working ok. Furthermore, I'm able to interface and use this library without any issues with another Fortran90 project. However, when I try to import the same in Python,
>>> from ctypes import *
>>> from os import environ, listdir
>>> import dp_py
I get a segmentation fault
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Based on this answer I tried working with the python debugger. Towards the very end I see this output:
--Call--
> <frozen importlib._bootstrap_external>(919)create_module()
(Pdb)
> <frozen importlib._bootstrap_external>(921)create_module()
(Pdb)
> <frozen importlib._bootstrap_external>(922)create_module()
(Pdb)
--Call--
> <frozen importlib._bootstrap>(211)_call_with_frames_removed()
(Pdb)
> <frozen importlib._bootstrap>(219)_call_with_frames_removed()
(Pdb)
Segmentation fault: 11
Whats being done in <frozen importlib._bootstrap>(219)_call_with_frames_removed()
that results in a segmentation fault? How could I resolve this?
The python interpreter I use is:
Python 3.6.8 :: Anaconda custom (64-bit)
It turns out that including the global objects with extern const
was the problem. Not including global objects now solves the error.