11

I've recently learned how to program simple character drivers and while playing around with the code I noticed that I get a lot of the following GCC warnings thrown for my C99 code:

warning: ISO C90 forbids mixed declarations and code

I assume this is because the main Linux kernel Makefile is set to compile using a non-C99 standard. I searched around I found this answer here on stackoverflow: How to use make and compile as C99?

So I naturally tried the following in my Makefile:

ccflags-y := -std=gnu99

Unfortunately this didn't silence the GCC warnings. I checked the verbose output of make and verified that GCC is indeed executed with the -std=gnu99 tacked on at the end; so I'm a bit confused.

How do I properly compile a Linux kernel module using the -std=gnu99 option?

EDIT:

I noticed the GCC output shows this option: -Wdeclaration-after-statement. Is this why I am getting the warnings even with the -std=gnu99 option?

Community
  • 1
  • 1
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
  • The linux makefile is intially called before your module, so GCC might get mixed std= definitions. – stdcall Apr 09 '13 at 20:27
  • 4
    Why don't you just declare all the variables at the beginning of the function and get rid of the warning that way? – Gonzalo Apr 09 '13 at 20:29
  • 1
    This is probably because that feature of C99 (declaration after statement) is one of the things that Linus seems to hate. So the linux coding style forbids this. – Jens Gustedt Apr 09 '13 at 21:02
  • @JensGustedt That's odd, I vaguely remember that being mention in the [Linux kernel coding style](https://www.kernel.org/doc/Documentation/CodingStyle) document, but I can't seem to find it anymore. – Vilhelm Gray Apr 11 '13 at 12:41
  • [dupe](http://stackoverflow.com/questions/2935047/how-to-use-make-and-compile-as-c99)? you do however have a better possible answer. – Ciro Santilli OurBigBook.com Jun 23 '13 at 14:46
  • 1
    @Gonzalo you'd be out of luck trying to do it when you want to make a constant at the right time, like `const int numTimesFooDone=doFoo();`. You'll have to leave `numTimesFooDone` non-`const` and risk mistakenly overwriting it without noticing somewhere later, e.g. `if(numTimesFooDone=3)...`. This just takes away some useful compiler help from the programmer. – Ruslan Jan 06 '16 at 18:14

2 Answers2

21

It turns out that -std=gnu99 does in fact work; I began seeing errors regarding C99 features after removing the compiler flag. So that meant something else was causing the warnings to print out besides the -std= option.

After parsing through the verbose output via make V=1, I discovered the -Wdeclaration-after-statement option as part of the GCC execution. This was the cause of the ISO C90 mixed declaration warnings I saw.

To disable the ISO C90 warnings, pass this to GCC: -Wno-declaration-after-statement.

For example:

ccflags-y := -std=gnu99 -Wno-declaration-after-statement
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
0

You can also specify the flag in your Makefile, if you have one:

FLAGS=-std=gnu99
Marshall Shen
  • 1,323
  • 12
  • 17