在内核模块中预定义宏 [英] Predefine a macro in kernel module

查看:99
本文介绍了在内核模块中预定义宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用-D标志为我的内核模块定义一个宏,但是我不知道如何在定制的可加载内核模块中执行该操作.

I would like to define a macro for my kernel module by using the -D flag, but I can't figure out how to do it in a custom loadable kernel module.

请明确一点,要将宏TEST设置为1,我通常会执行以下操作:
cc -D TEST=1 file.c -o file

Just to be clear, to set the macro TEST to 1 I usually do something like:
cc -D TEST=1 file.c -o file

在file.c里面,我有

And inside the file.c I have

#if TEST
   //do something
#endif

现在,在内核模块中具有相同的代码,如何在不触摸代码的情况下将TEST设置为1?

Now, having the same code in a kernel module, how can I set TEST to 1 without touching the code?

这是我的Makefile:

This is my the Makefile:

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

由于-C标志正在递归调用多个生成文件,因此添加-D TEST=1不起作用,我得到以下错误: make: invalid option -- 'D'

Since the -C flag it's recursively calling multiple makefiles, adding -D TEST=1 does not work, I get the following error: make: invalid option -- 'D'

有人知道如何解决这个问题吗?

Anybody knows how to solve this problem?

谢谢.

推荐答案

如@ n.m所建议.在注释中,解决方案是使用EXTRA_CFLAGS.
因此,在我的情况下,应该是这样的:

As suggested by @n.m. in the comments, the solution is to use the EXTRA_CFLAGS.
So in my case it would be something like this:

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

或简单地

EXTRA_CFLAGS:= -D TEST=2

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

这篇关于在内核模块中预定义宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆