简体   繁体   中英

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

For example, the line below compiles ok with gcc,

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

but g++ complains about the pointer type mismatch like below.

../../../../../YOLO/darknet/src/gemm.c:33:22: error: invalid conversion from 'void*' to 'float*

(maybe callc always returns void* in c++?)
Can I make g++ just ignore this pointer type mismatch error? (I found this link but they say it's unavoidable. If we can use c code from inside c++ code without fixing this everywhere, it would be nice.)

(maybe callc always returns void* in c++?)

Yes. calloc always returns void* . Both in C and C++.

Can I make g++ just ignore this pointer type mismatch error?

I recommend to not attempt making the compiler to ignore the bug, but to fix the program instead. You can fix it like this:

float *m = static_cast<float*>(std::calloc(rows*cols, sizeof(float)));

Or compile the translation unit with a C compiler if it is written in C.

If we can use c code from inside c++

Solution: Don't use one language inside another. Write C in C and C++ in C++. Interaction between the languages is possible by linking the translation units together.

The solution to this problem is actually not to add a static_cast as eerorika suggests but compiling the C code with a C compiler. There are a number of subtle differences between C and C++ that can lead to unexpected results, and the compiler won't catch them all. So even if you change all the type warnings you might still end up with broken code.

To ensure that you can call the C code from C++ you should mark the code as extern "C" inside the C headers like so:

#ifdef __cplusplus
extern "C" {
#endif

[your definitions]

#ifdef __cplusplus
}
#endif

Regarding C++, read its draft standard n3337 or the newer n4860 draft or buy the official standard from ISO (or the equivalent standardizing organization in your country).

Regarding C, read its draft standard n1570 or buy the official standard from ISO.

For both C and C++, refer also to this website .

Read also this Modern C book about C, and that Programming -- Principles and Practice Using C++ book about C++.

If you are using a recent GCC , and if you are allowed to code some plugin for it, you might code a plugin which conditionally would correct the source code, or suggest and send some correction to your favorite editor (eg GNU emacs ). Such an approach could take many weeks of effort.

As this answer correctly explains, you should improve your source code.

PS. I happen to be capable to write such a GCC plugin (see this draft tech report funded by the CHARIOT European project), but I need to get funded to do so. Feel free to contact me by email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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