mardi 2 février 2021

Pybind11 C++ module issues

I am trying to get a minimal example running calling c++ routines from python. I am referring to a related blog here. I am not able to get any output from the c++ code. Also I am not getting any error or warning which can guide me.

max.cpp

#include <iostream>
using namespace std;

// function declaration
int max(int num1, int num2);

int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
   int ret;

   // calling a function to get max value.
   ret = max(a, b);
   cout << "In CPP : Max value is : " << ret << endl;

   return 0;
}

// function returning the max between two numbers
int max(int num1, int num2) {
   // local variable declaration
   int result;

   if (num1 > num2)
      result = num1;
   else
      result = num2;

   return result;
}

tasks.py

import invoke

@invoke.task()
def build_max(c):
    "Build the shared library for cpp cmult code"
    print_banner("Building C++ Library")
    invoke.run(
        "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC max.cpp "
        "-o libmax.so"
    )
    print("* Complete")

def compile_python_module(cpp_name, extension_name):
    invoke.run(
        "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
        "`python3 -m pybind11 --includes` "
        "-I /usr/include/python3.8 -I .  "
        "{0} "
        "-o {1}`python3.8-config --extension-suffix` "
        "-L. -lmax -Wl,-rpath,.".format(cpp_name, extension_name)
    )

@invoke.task(build_max)
def build_pybind11_max(c):
    """ Build the pybind11 wrapper library """
    print_banner("Building PyBind11 Module")
    compile_python_module("pybind11_max.cpp", "pybind11_max")
    print("* Complete")

@invoke.task()
def test_pybind11_max(c):
    """ Run the script to test PyBind11 """
    print_banner("Testing PyBind11 Module")
    invoke.run("python3.8 pybind11_max_test.py", pty=True)

max.hpp header file

int main();

pybind11 code(pybind11_max.cpp) that wraps C++ code

#include <pybind11/pybind11.h>
#include <max.hpp>

PYBIND11_MODULE(pybind11_max, m) {
    m.doc() = "pybind11 max plugin";
    m.def("cpp_function", &main, "find max");
}

I am able to compile and build it invoke build-pybind11-max

Response

==================================================
= Building C++ Library 
* Complete
==================================================
= Building PyBind11 Module 
* Complete

Code to test

#!/usr/bin/env python
import pybind11_max

if __name__ == "__main__":
    answer = pybind11_max.cpp_function()
    print(f"    In Python {answer}")

To run the test, I run invoke test-pybind11-max I get

==================================================
= Testing PyBind11 Module 

Adding updates here-

when I run the test file python pybind11_max_test.py from terminal, I get Segmentation fault (core dumped) error.

When running python3.8 pybind11_max_test.py, I get the following output

Fatal Python error: _PyArgv_AsWstrList: memory allocation failed
Python runtime state: initialized

Current thread 0x00007fc24d885080 (most recent call first):
  File "pybind11_max_test.py", line 5 in <module>

Aucun commentaire:

Enregistrer un commentaire