tags:

views:

2046

answers:

8

This has just come up as a question where I worked so I did a little digging and the answer is a ExpertsExchange one. So I hand you over to the original question asker, Manchung:

I have a project written in pure C which is to be used in embedded system. So, I use pure C to minimize the code size.

When I compile the project, I use the -ansi flag in order to make sure the code complies with the ANSI standard. However, the down side of using this ansi flag is that I am only allowed to use C styled comments (/*comments */). This is giving me a headache when I need to use nested comments.

So, my question is: what switches/flags can I use to allow me to use C++ styled comments (// comments) while keeping the ANSI checking enabled at the same time?

Thank you all very much!

Which pretty much sums my question up too.

G.

A: 

C has had C++ style comments for nearly ten years now, maybe you should upgrade?

1800 INFORMATION
I think we are using gcc 3.x supplied by Wind River for 64bit embedded systems
graham.reeds
That's what he's trying to do - upgrade from C89 to C89 with C++-style comments. Given that he said the target is an embedded system, it's unlikely that it has a C99-compliant compiler anyway. Even GCC doesn't implement C99.
Steve Jessop
Actually it turns out to be gcc 2.9!!
graham.reeds
If the code will only ever need to be compiled on gcc of version 2.9+, there's not much point using the -ansi flag. That stops you using things that other C89 implementations might be lacking, but if that's not a requirement then consider just using the default mode, -std=g89, which does permit //.
Steve Jessop
+9  A: 

You can use -lang-c-c++-comments preprocessor to have both ANSI mode and C++-style comments.

gcc -Wp,-lang-c-c++-comments -c source.c
philippe
That apparently doesn't work according to my colleague.
graham.reeds
That's because it's actually a preprocessor flag, so you invoke it like: gcc -Wp,-lang-c-c++-comments -- not directly on the command line. However, if you really want ansi portability then you shouldn't have c++ style comments... :)
Jason Coco
i think yours is the definitive answer.
Johannes Schaub - litb
I believe that this is no longer supported. I cannot get it to work in GCC 4.2 or 4.4 on either Mac or Linux. I can only find this reference in GCC's CPP manual (going back to version 2.9) "Note: Previous versions of cpp accepted a `-lang` option which selected both the language and the standards conformance level. This option has been removed, because it conflicts with the `-l` option." I cannot find a replacement.
Matt B.
+9  A: 

On recent releases of gcc, -ansi is documented as being the same as -std=c89. The new comment syntax is only available with the C99 standard, so -std=c99 would allow it.

There is also -std=gnu89, which is the same as -std=c89 but allowing all gcc extensions (including the C++-style comment syntax, which was a GNU extension long before it was added to the standard).

Also look at the -pedantic flag, which could give you some useful warnings.

References:

CesarB
-std=gnu89 is probably the OP's best bet, -std=c99 has other consequences that may not be desirable.
Zack
+13  A: 

If you want to use C++ style comments merely because you want to comment out blocks, and get a headache about nesting /* ... */, you can use this technique:

#if 0
... code ...
#endif

which will actually also do the job.

Johannes Schaub - litb
Anything wrong with my answer? Please comment if you downvote. You cannot nest C-Style comments, and C++ style comments are not valid in C++89. This `#if 0` is pretty much the only way there to do it cleanly if you want to comment out *code*.
Johannes Schaub - litb
+3  A: 

If you use -ansi only to check compatiblity, then the easiest might be to prepare a script or makefile target which copies the complete source tree into a "ansi" folder while patching away the // comments. In pseudo-bash:

SRC=main.c blip.c blip.h
cp makefile ansi-src/
for F in $SRC do
# does not handle // in string literals or /**/ comments!
sed 's/\/\/.*//g' < $F >ansi-src/$F
done
cd ansi-src
make CFLAGS=-ansi

If your SCC supports it, this could also be made into a commit-hook which builds the code automatically on a dedicated machine in ANSI mode after each commit and mails you a report about it.

Luther Blissett
A: 

gcc -x cpp would force the files to be treated as c++ files, without renaming them.

AShelly
A: 

I'm afraid scripting up some kind of comment-rewrite utility (as Luther Blissett suggests) might be your only option here. In modern versions of gcc, there doesn't seem to be any sort of compiler flag or CLI option that will enable C++-style comments when -ansi or -std=c89 is used.

You might be able to get at what you want by approaching it from a different angle. You may be able to compile with -std=c99 and use compiler flags to disable the C99-specific extensions that you don't want (without knowing more about why exactly you need ANSI compliance, I can't recommend specific flags).

bta
A: 

You could automatically build a copy of the source with the comments stripped, and use that in your program.

E.g. this looks promising

HoboBen