2016-05-18 1 views
1

カーネルモジュールのコンパイル時に "DEBUG"という変数を定義しようとしています。コンパイル時にMakefileのパスを定義しますか?

つまり、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が次の後にpoducesすることを確認したいと思います。

を意図したとおりに実行するコードの下に作られ
#if (DEBUG == 1) 
printk(KERN_INFO "DEBUG = 1\n") 
#endif 
+0

'ccflags-y:= $(INCLUDE_DIRS)-DDEBUG'おそらく? – user657267

+0

レシピに直接 'make 'を書くことは絶対にしないでください。サブメイクを実行するには、常に変数 '$(MAKE)'を使用してください。 – MadScientist

+0

Btw、上記の例では、コンパイル時の挿入は最適ではありません。私は動的デバッグがどのように実装されているかを見てみることをお勧めします(ストレージの一部ではコードやもの)。 – 0andriy

答えて

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

を意図したとおりに

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

実行コードの下メイド:sudoはtest_module.ko

DEBUG = 1 
Hello world! 

ソリューションにinsmod

#if (DEBUG == 1) printk(KERN_INFO "DEBUG = 1\n") #endif 
関連する問題