I found some code here that I tried locally, it is all about calling a function from Python3 using pybind11. I successfully installed pybind11 however, my G++ compiler can't locate the Python.h module(since pybind11 need Python.h header), and so I searched here some solutions, some say try installing python3.8-dev, some also say try installing libpython3.8-dev, neither the two solutions worked.
I have no choice but to look for another solution, and there I found the use of -I/usr/include/python3.x and so I did:
g++ -I/usr/include/python3.8 main.cpp
I almost had it all until I found this weird looking error:
(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x4f): undefined reference to `PyFloat_Type'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x6e): undefined reference to `PyFloat_Type'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x76): undefined reference to `PyType_IsSubtype'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0xa1): undefined reference to `PyFloat_AsDouble'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0xd9): undefined reference to `PyErr_Occurred'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0xfc): undefined reference to `PyErr_Clear'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x11f): undefined reference to `PyNumber_Check'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x14a): undefined reference to `PyNumber_Float'
/usr/bin/ld: main.cpp:(.text._ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb[_ZN8pybind116detail11type_casterIdvE4loadENS_6handleEb]+0x174): undefined reference to `PyErr_Clear'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
I also tried:
g++ -I/usr/include/python3.8 main.cpp -lpython3.8m
and it says:
usr/bin/ld: cannot find -lpython3.8m
collect2: error: ld returned 1 exit status
I also tried:
g++ -o main main.py \
-I/usr/include/python3 \
$(python3-config --cflags) $(python3-config --ldflags)
and I got this:
/usr/bin/ld: /tmp/ccIRtkYI.o: relocation R_X86_64_32S against undefined symbol `_Py_TrueStruct' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status
I also tried:
g++ -o main main.py \
-I/usr/include/python3 \
$(python3-config --cflags) $(python3-config --ldflags) fPIE
and got this:
/home/ystyphn/.local/lib/python3.8/site-packages/pybind11/include/pybind11/cast.h:1071: undefined reference to `PyErr_Occurred'
/usr/bin/ld: /home/ystyphn/.local/lib/python3.8/site-packages/pybind11/include/pybind11/cast.h:1076: undefined reference to `PyErr_Clear'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
Update: The code is:
#include <pybind11/embed.h>
#include <iostream>
namespace py = pybind11;
int main() {
py::scoped_interpreter python;
auto math = py::module::import("math");
double root_two = math.attr("sqrt")(2.0).cast<double>();
std::cout << "The square root of 2 is: " << root_two << "\n";
}
It is from the thread: call a Python function from c++ using pybind11
And YES after all I still like programming, hoping someone will see this
UPDATE: I created CMakeLists file instead
cmake_minimum_required(VERSION 3.4)
project(example)
set(CMAKE_LANGUAGE_COMPILER "/usr/bin/c++")
add_subdirectory(pybind11)
add_executable(example main.cpp)
target_link_libraries(example PRIVATE pybind11::embed)
And I downloaded the pybind11 from github, and I included it as a subfolder to my example