我正在尝试将一个简单的(测试)Python脚本嵌入到C中。请参考以下问题:Undefined reference to main
error when embedding Python in C++我尝试在C中嵌入Python。这是我的Python文件(名称为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)
这是我的C++文件(名称为EmbedTest.cpp,位于与www.example.com相同的文件夹中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; }
我尝试通过运行以下命令来编译和链接
gcc -c $(python3.8-config --cflags --embed) EmbedTest.cpp
gcc EmbedTest.o $(python3.8-config --embed --ldflags)
其中python3.8-config --cflags
扩展为
-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
并且其中python3.8-config --ldflags
扩展为
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
但是,我收到以下错误消息:
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
我知道这与链接时间优化有关,而且Python代码似乎是用gcc的旧版本编译的。我的gcc版本是8.3.0。为了解决这个问题,我尝试重新编译,但删除了任何额外的标志(与LTO有关)。所以我尝试
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)
但是我得到了和以前一样的错误信息。2有什么办法可以解决这个问题吗?
2条答案
按热度按时间3okqufwl1#
使用
-fno-lto
关闭LTO解决了此问题。3ks5zfa02#
当我在CMakeList.txt中错误地指定PYTHON_INCLUDE_DIR和PYTHON_LIBRARIES时,也遇到了同样的问题。当我删除手动指定的位置时,问题就解决了。