Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 954
  • Last Modified:

gcc compiler: How do I enable C++ styled comments while leaving ANSI enabled

Hi experts:

I am using the following version of gcc:
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

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!
2 Solutions
Hi manchung,

gcc -c -Wp,-lang-c-c++-comments myprog.c

From here: http://www.delorie.com/djgpp/v2faq/faq8_4.html

manchungAuthor Commented:
Hi gripe,

Thanks very much for your quick response.

I tried adding the new flag to the CFLAGS variable in "configure.in":
  CFLAGS="$CFLAGS -ansi -Wall -Wp,-lang-c-c++-comments -Werror-implicit-function-declaration -D_GNU_SOURCE -DHAVE_GLIB -DLINUX"

However, when I ran ./configure, it erred out and the log file said:
"configure: exit 77"

Any idea?

may be that your configure splits the CFLAGS from configure.in at -
Then I'd change CFLAGS in the Makefile after running configue (without that additional option in configure.in)
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

I think you can get most of what you want if you replace your "-ansi" switch with "-std=C99".  The C99 standard is the most recent version of the ANSI standard, and one of its new features is that it permits c++ style comments.  See http://gcc.gnu.org/gcc-3.3/c99status.html for details of gcc's support for C99.
manchungAuthor Commented:
Hi NovaDenizen,

Thanks a lot for your suggestion.

With the "-ansi" switch, the compiler disallows the the use of declarations (e.g. int num;) in the middle of a block. In other words, it only allows declarations at the very beginning of a block. This is one of the features that I want.

However, with the "-std=c99" switch, the compiler does allow declarations to appear anywhere.

Is there a way to keep the strict checking? Thanks again!
I don't think you can pick and choose gcc features like that anymore. It used to be that when you used -ansi, that would be equivalent to picking a certain set of -f options that turned on and off certain language features.  Then you could just manually use that same set of options with a few changes that you wanted.  Now, I think, gcc doesn't have that flexibility.  You pretty much have to go all-or-nothing.  

Maybe you should find a way to automatically change your // comments into /* */ comments.  Then you could just use -ansi.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now