Is there a way on one or more compilers to enforce only using a C++-compatible subset of C? (Would be most interested in gcc.)
e.g. int* p1 = malloc(10 * sizeof(int));
is not C++ code, but is C code, so I would want try to compile it C but have it give a warning or error that it isn't valid C++ code. Or is the solution to simply have two build options: One for c++, one or c, and try it twice.
Yes, that's why I said it wasn't C++ :)
To clarify: That example was an example of something that I would want the compiler to flag for me.
I'm starting to think the easier solution would be to just have two build setups (one for C, one for C++) instead of finding some obscure compiler setting, but still curious if there is one.
One idea is to set up your build system in such a way that it compiles as both C and C++ at the same time.
Otherwise, if you are more familiar with one of the languages it's probably a good idea to compile in the other language, because then you are more likely to get helped by the compiler. If you are unsure about something, look it up. From time to time, compile in your best language to verify that it actually works.
While it's perfectly reasonable to compile the same file twice (once in C mode and once in C++ mode - e.g. gcc -x c++ file.c), as a useful sanity check, that will only flag C-isms that are invalid C++.
Unfortunately, there are C-isms that are valid, but different C++.
The best known one is the difference in the type of character literals:
printf("%zu", sizeof'a'); // prints 1 in C++, 4 (or whatever sizeof(int) is) in C
, but there are other, sneakier ones - I've seen such issues caused by differences of scoping rules of for-loops and structs between C and C++.
So in general, it is impossible to statically verify that a piece of code is written in C++-compatible subset of C.
It would make sense to put C and C++ in different compiler units
That's how it is normally done when a code base uses both C and C++, but the header files end up being shared between the two languages, and are subject to these compatiblity issues.
I'm starting to think the easier solution would be to just have two build setups (one for C, one for C++) instead of finding some obscure compiler setting, but still curious if there is one.
I can't imagine why such a setting would exist. The best way to see if code is valid C++ is to compile it with a C++ compiler.