我正在尝试传递一个名为DEBUG的#34;定义变量"在编译时为内核模块。
即提供与DEBUG相同的功能,但是在内核模块的Makefile中。
gcc -o foo -DDEBUG=1 foo.c
任何人都可以告诉我如何实现这一目标吗?
Makefile:
# name of the module to be built
TARGET ?= test_module
# How to pass this during compile time? (-D$(DEBUG) or something similar)
DEBUG ?= 1
#define sources
SRCS := src/hello.c
#extract required object files
OBJ_SRCS := $(SRCS:.c=.o)
#define path to include directory containing header files
INCLUDE_DIRS = -I$(src)/includes
ccflags-y := $(INCLUDE_DIRS)
#define the name of the module and the source files
obj-m += $(TARGET).o
$(TARGET)-y := $(OBJ_SRCS)
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
@echo "insert module:\n\t sudo insmod $(TARGET).ko"
@echo "remove module:\n\t sudo rmmod $(TARGET)"
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
我正在使用link中的模块(在init函数中稍作修改,请参阅#if #endif语句)
hello.c:
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lakshmanan");
MODULE_DESCRIPTION("A Simple Hello World module");
static int __init hello_init(void)
{
#if (DEBUG == 1)
printk(KERN_INFO "DEBUG = 1\n")
#endif
printk(KERN_INFO "Hello world!\n");
return 0; // Non-zero return means that the module couldn't be loaded.
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(hello_init);
module_exit(hello_cleanup);
我希望看到dmesg在
之后产生以下内容sudo insmod test_module.ko
DEBUG = 1
Hello world!
解决方案:
ccflags-y := -DDEBUG=$(DEBUG)
使下面的代码按预期执行
#if (DEBUG == 1)
printk(KERN_INFO "DEBUG = 1\n")
#endif
答案 0 :(得分:0)
ccflags-y := -DDEBUG=$(DEBUG)
使下面的代码按预期执行
#if (DEBUG == 1)
printk(KERN_INFO "DEBUG = 1\n")
#endif