I am trying to embed a simple (test) Python script into C++. See this question for reference: Undefined reference to `main` error when embedding Python in C++ I'm trying to embed Python in C++. This is my Python file (with the name EmbedTest.py):
from __future__ import division
class model:
def init(self,a,b):
self.a = a
self.b = b
def test_method(a,b):
m = model(a,b)
m.add(1)
print("a: ",a,"b: ",b)
return (a+b,a-b,a*b)
This is my C++ file (with the name EmbedTest.cpp and located in the same folder as EmbedTest.py)
#include <Python.h>
int main(int argc, char *argv[]) {
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue, *pValue_1, *pValue_2;
double sum,diff,prod;
double a = atof(argv[1]);
double b = atof(argv[2]);
Py_Initialize();
pName = PyUnicode_DecodeFSDefault("EmbedTest.py");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if(pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule,"test_method");
if(pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(2);
pValue_1 = PyFloat_FromDouble(a);
pValue_2 = PyFloat_FromDouble(b);
if (!pValue_1) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
if (!pValue_2) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
PyTuple_SetItem(pArgs, 0, pValue_1);
PyTuple_SetItem(pArgs, 1, pValue_2);
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
sum = PyFloat_AsDouble(PyTuple_GetItem(pValue,0));
diff = PyFloat_AsDouble(PyTuple_GetItem(pValue,1));
prod = PyFloat_AsDouble(PyTuple_GetItem(pValue,2));
printf("a: %f b: %f sum: %f diff: %f prod: %f",a,b,sum,diff,prod);
Py_DECREF(pValue);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
} else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return 1;
}
if (Py_FinalizeEx() < 0) {
return 120;
}
return 0; }
I try to compile and link by running
gcc -c $(python3.8-config --cflags --embed) EmbedTest.cpp
gcc EmbedTest.o $(python3.8-config --embed --ldflags)
where python3.8-config --cflags
expands to
-I/home/MyUserName/anaconda3/include/python3.8 -I/home/MyUserName/anaconda3/include/python3.8 -Wno-unused-result -Wsign-compare -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O3 -ffunction-sections -pipe -isystem /home/mbm/anaconda3/include -fdebug-prefix-map=/tmp/build/80754af9/python_1593706424329/work=/usr/local/src/conda/python-3.8.3 -fdebug-prefix-map=/home/MyUserName/anaconda3=/usr/local/src/conda-prefix -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -flto -DNDEBUG -fwrapv -O3 -Wall
and where python3.8-config --ldflags
expands to
python3.8-config --ldflags
-L/home/MyUserName/anaconda3/lib/python3.8/config-3.8-x86_64-linux-gnu -L/home/MyUserName/anaconda3/lib -lcrypt -lpthread -ldl -lutil -lrt -lm -lm
However, I get the following error message:
lto1: fatal error: bytecode stream in file ‘/home/MyUserName/anaconda3/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a’ generated with LTO version 6.0 instead of the expected 7.1
compilation terminated.
lto-wrapper: fatal error: gcc returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
I understand this has to with the link-time-optimization and the fact that the python code appears to be compiled with an old vesion off gcc. My version of gcc is 8.3.0. To address this I tried to recompile but removing any extra flags (that have to do with LTO). So I tried
gcc -c -I/home/MyUserName/anaconda3/include/python3.8 -I/home/MyUserName/anaconda3/include/python3.8 EmbedTest.cpp
gcc EmbedTest.o $(python3.8-config --embed --ldflags)
but I get the same error message as before. Any idea how to solve this problem?
Turning off LTO with
-fno-lto
solved it.