简体   繁体   English

将 Python 嵌入到 C++ 接收错误分段错误(核心转储)

[英]Embedding Python to C++ Receiving Error Segmentation fault (core dumped)

This is my first go at Embedding Python in C++.这是我第一次尝试在 C++ 中嵌入 Python。

I am just trying to create a simple program so I understand how it works.我只是想创建一个简单的程序,以便了解它是如何工作的。

The following is my code.以下是我的代码。

main.cpp主文件

#define PY_SSIZE_T_CLEAN
#include </usr/include/python3.8/Python.h>
#include <iostream>


int main(int argc, char *argv[]){
    
    PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;

    Py_Initialize();


    pName = PyUnicode_FromString((char*)"script");
    pModule = PyImport_Import(pName);
    pFunc = PyObject_GetAttrString(pModule, (char*)"test");
    pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"Greg"));
    pValue = PyObject_CallObject(pFunc, pArgs);
    
    auto result = _PyUnicode_AsString(pValue);
    std::cout << result << std::endl;

    Py_Finalize();

      

}

script.py脚本.py

def test(person):
    return "What's up " + person;

This is how I have been compiling on Linux这就是我在 Linux 上编译的方式

g++ -I/usr/include/python3.8/ main.cpp -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -lpython3.8 -o output

I am compiling like this because (#include <Python.h> has been giving me troubles, Yes I have tried sudo apt-get install python3.8-dev)我这样编译是因为(#include <Python.h> 一直给我带来麻烦,是的,我已经尝试过 sudo apt-get install python3.8-dev)

The file compiles successfully but when I try to run ./output I receive the following error.该文件编译成功,但是当我尝试运行 ./output 时,我收到以下错误。

Segmentation fault (core dumped)

I searched up what this error means and it is saying that Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”我搜索了这个错误是什么意思,它说分段错误是由访问“不属于你”的内存引起的一种特定错误。

But which memory does not belong to me?但哪段记忆不属于我? Is it the python file?是python文件吗?

Any Guidance would be much appreciated.任何指导将不胜感激。

After each and every one of those statements, you will need to check for errors, using something of the form:在这些语句中的每一个之后,您都需要使用以下形式检查错误:

if (varname == NULL) {
    cout << “An error occured” << endl;
    PyErr_Print();
    return EXIT_FAILURE;
 }

This will check if the python layer through an error;这将检查python层是否通过错误; and if so will ask it to print the Python traceback to the screen, and exit.如果是这样,它将要求它将 Python 回溯打印到屏幕上,然后退出。 You can use this traceback to figure out what your error is.您可以使用此回溯来找出您的错误是什么。

Any of those functions can fail, and you need to check for failure before you continue.这些功能中的任何一个都可能失败,您需要在继续之前检查是否失败。 Using the Python C API is extremely fiddly because of this.因此,使用 Python C API 非常繁琐。 Most C API functions that return a pointer return NULL on error, and passing NULL into any function without checking it first is bound to result in a crash.大多数返回指针的 C API 函数在出错时返回 NULL,并且将 NULL 传递给任何函数而不首先检查它必然会导致崩溃。

You get a segmentation fault from accessing a NULL pointer, as nearly all modern systems map access of NULL to a segmentation fault or crash of some sort to catch programming errors.访问 NULL 指针会导致分段错误,因为几乎所有现代系统都将 NULL 的访问映射到分段错误或某种类型的崩溃以​​捕获编程错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM