0

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.

9
  • "when I include <boost/python.h>" -- Your code includes <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). Commented Nov 11, 2024 at 21:07
  • 1
    Please include the full error message. In particular, it's very relevant where the error occurs and MSVC also includes an error code (Cxxxx or LNKyyyy) which server as key for research. Commented Nov 11, 2024 at 21:14
  • From the second page of the tutorial to which you linked: "Experience shows that 90% of the 'I can't build Boost.Python' problems come from people who had to use a different tool [than bjam]." Are you sure you want to use Visual Studio instead of bjam? Commented Nov 12, 2024 at 2:36
  • 1
    Before taking this route, have you investigated whether numpy or scipy could run your equations? Both of those are standard modules that are mathematics-heavy and are implemented in C or Fortran for speed. Commented Nov 12, 2024 at 2:48
  • Boost can be heavy and finicky to set up. I recommend trying pybind11 instead. Commented Nov 12, 2024 at 4:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.