3

I am using the MinGW compiler for Windows. I am making some programs in C. Most of the articles I read up on this seem to be outdated... last I read C99 was incomplete in the GCC is this still true? My real question is cross platform compatibility between setting C99 and GNU99... should I avoid using GNU99 setting and it's extensions and just stick with C99? I am new to this MinGW compiler set as I have always used Visual Studio and decided to try something new... right now I am compiling with these settings...

-march=native -O3 -std=gnu99

Is there any recommended compiler commands I should enter for making C programs and also for making C++ programs with this compiler?

I want to make a simple program that is compatible with Windows, Mac, & Linux but first most Windows.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
CodeCamper
  • 6,609
  • 6
  • 44
  • 94
  • So you tacked on C++ at the end of the question but for the most part it seems like you are concerned mostly with C and C99 in particular. Would you be planning on targeting C++11? – Shafik Yaghmour Jul 29 '13 at 12:55

3 Answers3

3

If you want something that compiles with "any" compiler, you should avoid the gnu99 setting, and use the c89, c99 or c11- and for C++ use c++03 or c++11 (or c++0x depending on which particular version of compiler it is). The later you go on these things, the more you restrict to "new versions of compilers".

The gnu99 means C99 with "GNU extensions", which means that code that compiles with this setting may not compile on other compilers.

You should definitely also apply -Wall, and I like to have -Wextra -Werror too. -Wall means enable (almost) all warnings, -Wextra means enable extra warnings beyond the ones in -Wall, and -Werror means that you HAVE to fix any warnings before you get any code ("treat warnings as errors").

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thanks for the tips! Added -Wall -Wextra -Werror to the end of my compiler settings for C and C++. Also decided to go c11 and c++11 since they are the official/newest and I like multi-threading anyway. – CodeCamper Jul 29 '13 at 01:54
  • 1
    Note that `gcc` will allow you to use some extension without warning unless you specify `-pedantic`, `VLA` being the first example that comes to mind. – Shafik Yaghmour Jul 29 '13 at 02:07
  • @ShafikYaghmour Won't C11 allow VLA anyway because of it being now optional? – CodeCamper Jul 29 '13 at 02:09
  • @CodeCamper For example if you specify `-std=c90` it will not warn you at all which may be surprising for many, although their docs do cover that. – Shafik Yaghmour Jul 29 '13 at 02:10
1

Most of C99 feature has been supported by gcc, for detail see Status of C99 features in GCC.

Some GNU extensions are handy to use. Whether choosing between C99 and GNU99 depends on if you are going to use other compilers. But if you are thinking about Visual Studio, it doesn't support C99, so sticking to C89 is the choice if you are going back to Visual Studio later.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    Honestly I want to get away from Visual Studio because my goal is not to exclusively make programs for Windows. My main goal is the easiest possible cross system compatibility. If I can compile C99 with open source/free solutions for Windows, Mac, and Linux then that is all that matters to me. Thanks for letting me know VS only supports C89 I did not know that. Maybe they will update to 1999 in 2019? – CodeCamper Jul 29 '13 at 01:45
  • @CodeCamper Personally, I don't think Visual Studio will support C99 even in 2019. On the other hand, gcc already supports part of C11 now. – Yu Hao Jul 29 '13 at 01:50
  • I made sure to change my compiler settings to C11 and C++11 since the future will only get more futuristic. – CodeCamper Jul 29 '13 at 01:55
  • @YuHao c99 support in Visual Studio has been announced by Microsoft http://blogs.msdn.com/b/vcblog/archive/2013/06/28/c-11-14-stl-features-fixes-and-breaking-changes-in-vs-2013.aspx – ouah Jul 29 '13 at 01:56
  • @ouah I figured they had enough time since 1999. – CodeCamper Jul 29 '13 at 01:59
  • 1
    @ouah: There's always been _some_ support for C99, e.g. `//` comments. But there still is no plan for full compliance. – MSalters Jul 29 '13 at 07:29
  • @MSalters You may say that, but it's more likely that VS supports `//` comment as an extension, not as a feature of C99, since it supported it before C99 was born if I remember correctly from some references. – Yu Hao Jul 29 '13 at 07:40
  • @YuHao: Same thing with declarations after statements. C99 didn't come out of thin air, it formalized many common extensions at the time and the committee members including Microsoft knew well in advance which features would make it into C99. – MSalters Jul 29 '13 at 07:49
1

With respect to C, Visual Studio until recently did not support C99 at all.

With respect to gcc you can find a detailed detailed writeup on which standard they support and the nooks and crannies involved. They also have a nice list of extensions they support. You need to be a little careful with gcc and extensions because just specifying which standard you want to use is not enough to generate a warning or error when you are using an extension. For example you might be surprised that using:

gcc -std=c90 -W -Wall

allows you to use variable length arrays without a warning. In order to generate a warning you need to add -pedantic:

gcc -std=c90 -W -Wall -pedantic

and then this will generate a warning similar to this:

warning: ISO C90 forbids variable length array ‘array’ [-Wvla]
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740