小编Xia*_*Luo的帖子

从C代码调用Cython函数会引发分段错误

我正在尝试在C程序中调用cython(cdef)函数。当cdef函数包含python语句(例如print(0.5)或python(def)函数)时,调用(cdef)函数会引发分段错误。

.pyx文件:

# cython: language_level=3

cdef public double PI = 3.1415926

cdef public double get_e():
    print("calling get_e()")
    return 2.718281828
Run Code Online (Sandbox Code Playgroud)

.c文件:

#include "Python.h"
#include "transcendentals.h"
#include <math.h>
#include <stdio.h>

int main(int argc, char **argv) {
  Py_Initialize();
  PyInit_transcendentals();
  printf("pi**e: %f\n", pow(PI, get_e()));
  Py_Finalize();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译命令:

cython transcendentals.pyx

gcc -I. -I/usr/include/python3.5m -I/usr/include/python3.5m \
-Wno-unused-result -Wsign-compare \
-g -fstack-protector-strong -Wformat \
-Werror=format-security -DNDEBUG -g \
-fwrapv -O3 -Wall -Wstrict-prototypes \
-L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu \
-L/usr/lib transcendentals.c main.c \
-lpython3.5m -lpthread -ldl -lutil -lm -Xlinker …
Run Code Online (Sandbox Code Playgroud)

c cython python-3.x

5
推荐指数
1
解决办法
252
查看次数

标签 统计

c ×1

cython ×1

python-3.x ×1