I have code in C++ that performs multiple mathematical equations. When doing it with Python, it takes a long time (about 5 minutes), and when running it in C++, it takes about 10 seconds. I want to have Python use the C++ functions. I read that with the Boost.Python library, this task could be done, but I am having problems when compiling the C++ program.
The first error I encounter is that, after including the boost/python library as follows:
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
char const* greet()
{
return "hello, world";
}
I get the following error:
BOOST_PYTHON_MODULE_INIT namespace “boost::python::detail” has no “init_module” member.
This is the tutorial link that i use Boost.Python Tutorial
I find the documentation very confusing. What I have tried so far is to use Visual Studio 2022, start a new project and select a dll file. When I include <boost/python.hpp> I don't get any error, but when I put the BOOST_PYTHON_MODULE(hello_ext) line I get the error mentioned above.
=========================== EDIT ===========================
In the Boost.python tutorial, I tried to run the example using bjam but it does not recognize it as an internal or external command in cmd, I looked for a way to install it but according to what I found, there should be an executable called bjam in the base folder of boost, I only found a file called Jamroot but I don't know what to do with it.
Apart from this, in the example folder it says that it should have a list of files:
- hello.cpp
- hello.py
- Jamroot
But instead of the jamroot file, there is only a file called Jamfile, in which the following content is found:
Jamfile
import python ;
project tutorial
: requirements
<location>.
;
python-extension hello_ext : hello.cpp ;
run-test hello : hello_ext hello.py ;
alias test : hello ;
explicit test ;
hello.cpp
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
hello.py
import hello_ext
print(hello_ext.greet())
I really started using visual studio out of ignorance, but now that I've reached this point, I don't understand how to install bjam or how to configure the jamroot file specified in the tutorial, nor do I understand what to do with that jamfile.
And I necessarily have to use c++, it is not an option to migrate all the code to pure python since it was tried before and did not meet the expected time.
<boost/python.hpp>not<boost/python.h>. I think this is just sloppiness in your text (and I don't think this is the cause of your problem), but please double-check and correct your question (including the title).