0

my cpp file is this:

#include <pybind11/pybind11.h>
#include <iostream>

namespace py = pybind11;

int main() {
    auto math = pybind11::module::import("math");
    auto resultobj = math.attr("sqrt")(2);
    double result = resultobj.cast<double>();

    std::cout << "The square root of 2 is: " << result << "\n";
}

when I try to compile it using this command:

g++ -Wall -std=c++11 -fPIC -I/home/qwert/miniconda3/include -I/home/qwert/miniconda3/include/python3.9 fn_call.cpp

I get a lot of errors which I have not been able to fix

first few lines of errors:

/usr/bin/ld: /tmp/cc61RVdo.o: in function `_Py_DECREF':
fn_call.cpp:(.text+0x74): undefined reference to `_Py_Dealloc'
/usr/bin/ld: /tmp/cc61RVdo.o: in function `pybind11::type_error::set_error() const':
fn_call.cpp:(.text._ZNK8pybind1110type_error9set_errorEv[_ZNK8pybind1110type_error9set_errorEv]+0x2d): undefined reference to `PyExc_TypeError'
/usr/bin/ld: fn_call.cpp:(.text._ZNK8pybind1110type_error9set_errorEv[_ZNK8pybind1110type_error9set_errorEv]+0x3b): undefined reference to `PyErr_SetString'
/usr/bin/ld: /tmp/cc61RVdo.o: in function `pybind11::cast_error::set_error() const':
fn_call.cpp:(.text._ZNK8pybind1110cast_error9set_errorEv[_ZNK8pybind1110cast_error9set_errorEv]+0x2d): undefined reference to `PyExc_RuntimeError'
/usr/bin/ld: fn_call.cpp:(.text._ZNK8pybind1110cast_error9set_errorEv[_ZNK8pybind1110cast_error9set_errorEv]+0x3b): undefined reference to `PyErr_SetString'

I have tried reinstalling python, pybind11, and even trying it on a different linux machine

  • 1
    You need to link against python (https://stackoverflow.com/questions/3891202/how-to-link-python-static-library-with-my-c-program). But I recommend just going for CMake or setuptools and not trying to manually set the compiler flags. See https://pybind11.readthedocs.io/en/stable/compiling.html – unddoch Dec 10 '22 at 22:01

0 Answers0