If we want to write a module in C and have to compile it as C++ with g++
, is it OK to develop a piece of code in C++ without any own classes, only using "global / static functions" as in C? So, simply said, to code C in C++ (with only few system header changes etc.)
Yes. In fact, it's generally a good idea because C++ enforces stronger type-checking than C.
You will need to do a couple of things other than only use functions, in particular you should mark all your functions as extern "C"
to avoid name mangling and enforce C calling conventions (and incidentally block you from overloading). If you want to be able to compile it in C, you will have to qualify types with struct
when declaring variables (enum
for enumerations), or provide the appropriate typedefs...
Alternatively, you can add -x c
to the compiler options to tell g++ to compile the code as C (if you are not able to change the command line from g++
to gcc
, you might not be able to add compiler flags either...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With