How to use make and compile as C99?

主宰稳场 提交于 2019-12-03 04:51:32

It's got nothing to do with the makefile. ISO C90 forbids declaring variables anywhere but in the beginning of a block or the file - like this

int main(int argc, char **argv) {
   int a; /* Ok */
   int b = 3; /* Ok */

   printf("Hello, the magic number is %d!\n", b);
   int c = 42; /* ERROR! Can only declare variables in the beginning of the block */
   printf("I also like %d.. but not as much as %d!\n", c, b);

   return 0;
}

Thus it has to be modified to this...

int main(int argc, char **argv) {
   int a; /* Ok */
   int b = 3; /* Ok */
   int c = 42; /* Ok! */

   printf("Hello, the magic number is %d!\n", b);
   printf("I also like %d.. but not as much as %d!\n", c, b);

   return 0;
}

You can only "fix" that in the source code, not in the makefile.

This rule has been relaxed in C99, but in my opinion it's a good idea to separate variable definitions, declarations and initializations from the code below it :)

So to change your makefile to make it compile with C99, you need to change the Makefile in the "build" directory that your makefile is referencing, and add the "-std=c99" at the "gcc" line compiling the source file.

Job

The correct way to add compiler flags when compiling modules is by setting the ccflags-y variable. Like this:

ccflags-y := -std=gnu99

See Documentation/kbuild/makefiles.txt in the kernel tree for more information.

Note that I'm using the gnu99 standard instead of c99 since the Linux kernel heavily relies on GNU extensions.

You could just add

CFLAGS=-std=c99

To the top of your makefile, or you can make the code compliant with C90 (as LukeN suggests.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!