Warm tip: This article is reproduced from serverfault.com, please click

c++-编译C代码时如何使G ++忽略calloc指针类型错误

(c++ - how to make g++ ignore calloc pointer type error when compiling c code)

发布于 2020-12-03 08:53:35

例如,下面的行用gcc编译就可以了,

float *m = calloc(rows*cols, sizeof(float));

但是g ++抱怨指针类型不匹配,如下所示。

../../../../../YOLO/darknet/src/gemm.c:33:22:错误:从“ void *”到“ float *”的无效转换

(也许callc在c ++中总是返回void *?)
我可以使g ++忽略此指针类型的不匹配错误吗?(我找到了此链接,但他们说这是不可避免的。如果我们可以在c ++代码内部使用c代码而无需在任何地方都进行修复,那将是很好的。)

Questioner
Chan Kim
Viewed
0
user1502256 2020-12-03 17:11:32

解决此问题的方法实际上不是static_cast按照eerorika的建议添加,而是使用C编译器编译C代码。C和C ++之间存在许多细微的差异,这些差异可能导致意外的结果,并且编译器无法完全捕捉它们。因此,即使你更改了所有类型警告,你仍然可能会遇到代码损坏的情况。

为了确保你可以从C ++调用C代码,应将代码标记为extern "C"在C标头中,如下所示:

#ifdef __cplusplus
extern "C" {
#endif

[your definitions]

#ifdef __cplusplus
}
#endif