这种行为很奇怪,因为我可以不获取段错误
此外,我无法在某些机器上获得段错误。
如果您发现我的代码中的错误更好,但我还有其他更常见的问题:
假设我有库源代码strange.c和hte python脚本run.py,我得到了段错误:
icc -O3 -Wall -shared strange.c -o libstrange.so
python run.py
请注意,我可以在我的某台计算机中重现此错误
uname -m
:i868 但
中的不uname -m
:i868 或
uname -m
:x86_64 请在此处(tkf / ctypes_icc / source – Bitbucket)或以下查找代码集。 您可以找到Makefile和shell脚本来运行程序,并使用所有优化标志和编译器(gcc和icc)检查退出代码。该程序的原始版本是我的研究的模拟程序,但该程序只是一个无意义的程序。
strange.c:
typedef struct{
int num_n;
double dt, ie, gl, isyn, ssyn, tau1, tau2, lmd1, lmd2, k1_mean, k2_mean;
double *vi, *v0;
} StrangeStruct;
void
func(double * v0, double * vt, double dt,
double gl, double isyn, double ie, double isyn_estimate, int num_n)
{
int i;
for (i = 0; i < num_n; ++i){
v0[i] = vt[i] + dt + gl + isyn + ie + isyn_estimate;
}
}
int
StrangeStruct_func(StrangeStruct *self)
{
double isyn_estimate;
isyn_estimate =
self->ssyn * (self->lmd1 * self->k1_mean - self->lmd2 * self->k2_mean) /
(self->tau1 - self->tau2);
func(self->v0, self->vi, self->dt, self->gl, self->isyn,
self->ie, isyn_estimate, self->num_n);
return 0;
}
run.py:
from ctypes import POINTER, pointer, c_int, c_double, Structure
import numpy
c_double_p = POINTER(c_double)
class StrangeStruct(Structure):
_fields_ = [
("num_n", c_int),
("dt", c_double),
("ie", c_double),
("gl", c_double),
("isyn", c_double),
("ssyn", c_double),
("tau1", c_double),
("tau2", c_double),
("lmd1", c_double),
("lmd2", c_double),
("k1_mean", c_double),
("k2_mean", c_double),
("vi", c_double_p),
("v0", c_double_p),
]
StrangeStruct_p = POINTER(StrangeStruct)
ifnet_a2a2 = numpy.ctypeslib.load_library('libstrange.so', '.')
ifnet_a2a2.StrangeStruct_func.restype = c_int
ifnet_a2a2.StrangeStruct_func.argtypes = [StrangeStruct_p]
def func(struct):
ifnet_a2a2.StrangeStruct_func(pointer(struct))
if __name__ == '__main__':
ifn = StrangeStruct(
num_n=100, dt=0.1, gl=0.1, vrest=-60, ie=-3.7, th=-40,
ssyn=0.5, tau1=3, tau2=1,
)
v0 = numpy.zeros(ifn.num_n, dtype=float)
vi = numpy.zeros(ifn.num_n, dtype=float)
ifn.v0 = v0.ctypes.data_as(c_double_p)
ifn.vi = vi.ctypes.data_as(c_double_p)
func(ifn)
v0 + vi
答案 0 :(得分:3)
通常不可能混合使用gcc和icc编译的二进制文件(在这种情况下,python是使用gcc构建的)。您可以尝试使用icc“gcc compatibility”模式,该模式由-gcc-version标志设置。这可能会让它发挥作用,但你仍有可能遇到问题。