My problem is that I cannot get a function from my Python script on my C++ script.
I use Python/C API and my IDE is VScode. When i run the code, it stop at the specific line calling PyObject_GetAttrString()
This line into the task.json of VScode is used to build my code : g++ -IC:/Users/Martin/AppData/Local/Programs/Python/Python37-32/libs/python37.a main.cpp -LC:/Users/Martin/AppData/Local/Programs/Python/Python37-32/libs -lpython37
And this line to run it: .\a.exe (.exe auto created by VScode)
It's the first time I tried to use Python code through C++ code like that. I've already searched in forums and on another topics on stackoverflow, but after all I tried I don't get it. Here is the code:
C++ code : main.cpp (which I execute)
#include <iostream>
#include "C:/Users/Martin/AppData/Local/Programs/Python/Python37-32/include/Python.h"
using namespace std;
int main ()
{
cout << "Start \n";
Py_Initialize();
cout << "2\n"; PyObject* my_module = PyImport_ImportModule("mat");
cout << "3\n"; PyObject* my_function = PyObject_GetAttrString(my_module,"getfive");
cout << "4\n"; PyObject* my_result = PyObject_CallObject(my_function,NULL);
cout << "5\n"; double result = PyFloat_AsDouble(my_result);
cout << "6\n"; printf("My result is : %f",result);
cout << "7\n";
Py_Finalize();
return 0;
}
Python code : mat.py
def getfive():
print "python say 5 !"
return 5
def speak():
print "speak"
Output I except to have:
Start 1
2
3
python say 5!
4
5
6
My result is : 5
7
Output I really have:
Start 1
2
3
And here I do not really understand why it doesn't work on this line : "PyObject* my_function = PyObject_GetAttrString(my_module,"speak"); "
Thank you for reading this far, and much more if you're answering me!