How to solve: lto1: fatal error: bytecode stream in file '...' generated with LTO version 6.0 instead of the expected 7.1

How to solve: lto1: fatal error: bytecode stream in file '...' generated with LTO version 6.0 instead of the expected 7.1

我正在尝试将一个简单的(测试)Python 脚本嵌入到 C++ 中。请参阅此问题以供参考: I'm trying to embed Python in C++。这是我的 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,位于与 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;  }

我尝试通过运行

编译和link
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

我知道这与 link-time-optimization 以及 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)

但是我得到了和以前一样的错误信息。知道如何解决这个问题吗?

-fno-lto 关闭 LTO 解决了它。