Reputation: 159
I started a project in Python and I want to work with some image objects. I want to call some C++ functions in my Python's code. After some research, I decided to use the Python Boost library to call a C++ function in my Python code.
My Boost version is : libboost_python3-py36.so.1.65.1. and I am using Python v3.6.
I wrote my C++ code like this in my CppProject.cpp:
char const* myMethod() {
// do some operations ...
return "It is Done";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(CppProject) {
boost::python::def("getTryString", myMethod); // boost::python is the namespace
}
also, I created my CMakeLists.txt like this:
cmake_minimum_required(VERSION 2.8.3)
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs)
FIND_PACKAGE(Boost COMPONENTS python)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
PYTHON_ADD_MODULE(NativeLib CppProject)
FILE(COPY MyProject.py DESTINATION .) # See the whole tutorial to understand this line
and finally, this is my Python code in MyProject.py:
import NativeLib
# some preprocess
print (NativeLib.getTryString)
After writing the code, I created a directory with the name build. and in that directory, I ran this command:
cmake -DCMAKE_BUILD_TYPE=Release ..
After that and before I run my Python code, I did make it and finally, I ran my python's code but segmentation fault occurred!
Could someone help me with solving this error?
Upvotes: 0
Views: 91
Reputation: 409176
The compiler needs all symbols you use in your program to be declared before you use them. If you use a symbol which have't been declared it will give you an error because it doesn't know about it.
Now when you use BOOST_PYTHON_MODULE
, that symbol is unknown by the compiler, and the whole statement is therefore syntactically wrong.
You must include the Boost header files that defines the BOOST_PYTHON_MODULE
macro, as well as boost::python::def
.
Upvotes: 1