So, I have an assignment in a computer science class; we’re supposed to make a mini-shell for Linux. And the first project requirement says:
• Must be implemented in C. (NOT C++)
Unfortunately, it’s been years since I used C and not C++; heck, I haven’t used printf and scanf and the like since 1996 or thereabouts, only cout and cin and other stream operators. So I’m not sure I’ll be able to avoid everything that is C++ and not plain C. Is there a way to verify that my code is pure C and uses no C++ stuff? I’m using the Gnu C compiler (gcc).
OK, the filename is shell.c, and I’ve tried the -ansi and -pedantic flags. Thanks groman. So does that mean that if I do have C++ stuff in my file, it will complain?
gazpacho, I don’t have lint itself, but I do have lclint and splint, which are apparently just more recent versions of lint. It looks like they have -ansi flags too. Do they actually work for C++ files at all, or are they strictly C-only tools?
I really don’t know all that much about the C linting tools specifically just that they are used in part to check that the code is syntactically correct. I am sure that there are c++ linting tools.
Use “class,” “public,” “template,” and/or “namespace” as variable names. If it compiles at all, you’ve successfully “turned off” C++ compilation for that unit. This sort of thing (using a C++ keyword as a non-keyword) is one of the few cases where a valid ANSI C program isn’t a valid C++ program, so it’s a pretty good test that you’ve got it right.
Thanks for the idea of using lint (well, splint), gazpacho. It seems pretty good about ensuring strict ANSI C compatibility, and it also detects memory leaks and the like.
TimeWinder, that’s another good idea. I’ll try that too. Thanks!
The solutions given above are probably adequate for you, but I thought I’d toss in another. Try adding this to your code:
#ifdef __cplusplus
#error C++ compiler active!
#endif
One small advantage of this technique is that you can leave it as-is in your source, if desired, and forget about it. If you then move the source to another machine, and another compiler, you’ll be informed quickly when you build if you’re getting the wrong language. No test code needs to be added and removed.