繁体   English   中英

正确编译的代码(没有错误)给出分段错误核心转储

[英]Rightly compiled code(without error) giving Segmentation fault core dumped

I am using cython to use python functions in c++, which itself calls a c++ function, Everything compiled perfectly well, but in the end step the error occurs: Segmentation fault (core dumped) when running the out file, I know its a tough ask找出错误,但有人可以帮助我:

#include  <iostream>
#include  "Python.h"
#include  "./plugins/strat_plugin.h" //cython generated header file
int main(int argc, char  *argv[])
{
    PyRun_SimpleString("import sys\n" "import os"); 
PyRun_SimpleString("sys.path.append( os.path.dirname(os.getcwd()) +'/plugins/')");
int status=PyImport_AppendInittab("start_plugin", &initstrat_plugin);
if(status==-1){
    std::cout<<"error in appendinit"<<"\n";
    return -1;//error
} 
Py_Initialize();
PyObject *module = PyImport_ImportModule("strat_plugin");
if(module==NULL){
    PyErr_Print();
    Py_Finalize();
    std::cout<<"error in import"<<"\n";
    return -1;//error
}
    long long ans=0;
    std::list<int> a;
    ans=gen_fibonacci(a,1,100); //this is the cython function
    std::cout<<"ans: "<<ans;
    std::cout<<"\n";
}

编译:

g++-8 ./plugins/strat_plugin.c helper.cpp $(python-config --libs)  $(python-config --includes)  $(python-config --cflags)

strat_plugin.pyx 文件:

from libcpp.list cimport list
from test import test_sum

cdef public long long gen_fibonacci(list[int] &l,int ind,int  n):
    num = 3
    t1 = 0 
    t2 = 1
    nextTerm = 0
    i=1
    if ind==1:
        l.push_back(0)
        l.push_back(1)
        i=3
    if ind==2:
        l.push_back(1)
        i=2
    while i<n:
        nextTerm=t1+t2
        t1=t2
        t2=nextTerm
        if num>=ind:
            i=i+1
            l.push_back(nextTerm)
        num=num+1
    return test_sum(l)

这可以很好地使用以下命令编译: cython -2 strat_plugin.pyx生成 header 和 c 文件。

层.h:

#pragma once

#include <iostream>
#include <list>
long long sum(std::list<int> &);

策略.cpp

包括“strat.h”

使用命名空间标准;

long long sum(list<int> &l)
{
    long long s =0;
    for(list<int>::const_iterator i = l.begin(); i != l.end(); i++)
    s+= *i ;
    return s;
}

策略.pyx:

from libcpp.list cimport list

cdef extern from "strat.h":
    long long sum(list[int] &)

def test_sum(list[int] l):
    return sum(l)

设置.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("test", ["strat.pyx", "strategy.cpp"], language='c++',)]

setup(cmdclass = {'build_ext': build_ext}, ext_modules = ext_modules)

编译使用:

python3 setup.py build_ext --inplace

更多相关信息:我尝试用 gdb 调试它并得到这个:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a2569b in PyImport_GetModuleDict () from /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0

我真的没有得到错误有人能指出什么是错的吗?

删除test_sum (因为我没有代码),忽略溢出错误(您的 python 代码超出了while循环中 int 的范围),并将PyInit_strat_plugin更改为initstrat_plugin ,它对我来说很好。

暂无
暂无

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

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