Pass Makefile определить во время компиляции?

Я пытаюсь передать "определить переменную с именем DEBUG" во время компиляции для модуля ядра.

то есть обеспечивают ту же функциональность, что и 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

Я использую модуль по ссылке (с небольшим изменением в функции init, см. Оператор #iF#endif)

Привет:

#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

1 ответ

ccflags-y := -DDEBUG=$(DEBUG)

Сделал код ниже выполнить как задумано

#if (DEBUG == 1)
printk(KERN_INFO "DEBUG = 1\n")
#endif
Другие вопросы по тегам