如何像C99一样使用make和编译?

发布于 2024-09-03 14:05:42 字数 420 浏览 8 评论 0原文

我正在尝试使用 Makefile 编译 Linux 内核模块:

obj-m += main.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

这给了我:

main.c:54: warning: ISO C90 forbids mixed declarations and code

我需要切换到 C99。读完后我注意到我需要添加一个标志 -std=c99,不确定应该添加到哪里。

如何更改 Makefile 以便将其编译为 C99?

I'm trying to compile a linux kernel module using a Makefile:

obj-m += main.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Which gives me:

main.c:54: warning: ISO C90 forbids mixed declarations and code

I need to switch to C99. After reading I noticed I need to add a flag -std=c99, not sure where it suppose to be added.

How do I change the Makefile so it will compile as C99?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪化雨蝶 2024-09-10 14:05:42

编译模块时添加编译器标志的正确方法是设置 ccflags-y 变量。像这样:

ccflags-y := -std=gnu99

请参阅 Documentation/kbuild/makefiles.txt在内核树中获取更多信息。

请注意,我使用的是 gnu99 标准而不是 c99,因为 Linux 内核严重依赖于 GNU 扩展。

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.

愚人国度 2024-09-10 14:05:42

您可以只添加

CFLAGS=-std=c99

makefile 的顶部,或者您可以使代码兼容 C90(如 LukeN 建议的那样。)

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.)

世界和平 2024-09-10 14:05:42

和makefile没关系。 ISO C90 禁止在块或文件开头以外的任何地方声明变量 - 像这样

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;
}

因此它必须修改为这样...

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;
}

您只能在源代码中“修复”它,而不能在 makefile 中“修复”它。

这条规则在 C99 中已经放宽,但在我看来,将变量定义、声明和初始化与其下面的代码分开是个好主意:)

因此,要更改 makefile 以使其使用 C99 进行编译,您需要更改 Makefile makefile 引用的“build”目录,并在编译源文件的“gcc”行添加“-std=c99”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文