GCC 用户态&内核态 Makefile

转了一圈,今天再次回到C

网上一篇博文,个人感觉良心作品,故而拿来重新实现一遍,原作者原文有问题,我这里把他打通了

一.GCC Makefile

//hello.c

#include <stdio.h>

#include <stdlib.h>

void main(void)

{

#ifdef DEBUG

             printf("you ask for debug! ");

#endif

             printf("we must say goodbye ");

             return;

}

//Makefile

ifeq ($(DEBUG),y)

        CFLAGS := $(CFLAGS) -DDEBUG

endif

hello: hello.c

        $(CC) $(CFLAGS) $< -o $@

//result

# make

cc  hello.c -o hello

# ./hello

we must say goodbye

# rm hello

# make DEBUG:=y

cc  -DDEBUG hello.c -o hello

# ./hello

you ask for debug!

we must say goodbye

二.KBuild(kernel gcc) Makefile

//ville.c

/**

 * * Author : ville lee villelee1987@gmail.com

 * * Create Time : 2010/07/31

 * * Description : This module implements a virtual file system.

 * * Aimming at learning the theory of Linux VFS.

 * *

 * * Change Log :

 * *    version author  Date    Log

 * *

 * *

 * *

 * * */

#include <linux/module.h>

#include <linux/init.h>

#include <linux/kernel.h>

/**

 * * init function of the module

 * *

 * * */

static int __init

ville_init(void)

{

#ifdef DEBUG

             /* we give the debug message like: module name : function name : debug message */

             printk(KERN_ALERT "ville :ville_init: you ask for debug! ");

#endif

             printk(KERN_ALERT "ville :ville_init: ville module init! ");

             return 0;

}

static void __exit

ville_exit(void)

{

#ifdef DEBUG

             /* we give the debug message like: module name : function name : debug message */

             printk(KERN_ALERT "ville :ville_exit: you ask for debug! ");

#endif

             printk(KERN_ALERT "ville :ville_exit: ville module exit! ");

             return;

}

module_init(ville_init);

module_exit(ville_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("ville lee");

//Makefile

## if module is built in the kernel module

## system. Give the work to kbuild.

# provide the default value to module name and ccflags-y

ifeq ($(MODULE),)

        MODULE := ville

endif

ifeq ($(DEBUG),y)

        ccflags-y += -DDEBUG

endif

ifneq ($(KERNELRELEASE),)

        obj-m := $(MODULE).o

else

        KERNELDIR := /lib/modules/$(shell uname -r)/build

        PWD := $(shell pwd)

default:

        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

.PHONY:clean

clean:

        echo "cleaning ...."

        rm modules.order Module.symvers

                $(MODULE).ko $(MODULE).mod.c

                $(MODULE).mod.o $(MODULE).o

        echo "clean up"

//result

# make

make -C /lib/modules/4.13.0-17-generic/build M=/home/woodzcl/c_develop/KMF modules

make[1]: Entering directory '/usr/src/linux-headers-4.13.0-17-generic'

  CC [M]  /home/woodzcl/c_develop/KMF/ville.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /home/woodzcl/c_develop/KMF/ville.mod.o

  LD [M]  /home/woodzcl/c_develop/KMF/ville.ko

make[1]: Leaving directory '/usr/src/linux-headers-4.13.0-17-generic'

# insmod ville.ko

# rmmod ville

# cat /var/log/kern.log

Nov 22 22:05:35 ubuntu kernel: [ 1495.967374] ville :ville_init: ville module init!

Nov 22 22:05:39 ubuntu kernel: [ 1500.103238] ville :ville_exit: ville module exit!

# make clean

echo "cleaning ...."

cleaning ....

rm modules.order Module.symvers

      ville.ko ville.mod.c

      ville.mod.o ville.o

echo "clean up"

clean up

# make DEBUG:=y

make -C /lib/modules/4.13.0-17-generic/build M=/home/woodzcl/c_develop/KMF modules

make[1]: Entering directory '/usr/src/linux-headers-4.13.0-17-generic'

  CC [M]  /home/woodzcl/c_develop/KMF/ville.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /home/woodzcl/c_develop/KMF/ville.mod.o

  LD [M]  /home/woodzcl/c_develop/KMF/ville.ko

make[1]: Leaving directory '/usr/src/linux-headers-4.13.0-17-generic'

# insmod ville.ko

# rmmod ville

# cat /var/log/kern.log

Nov 22 22:08:40 ubuntu kernel: [ 1680.874360] ville :ville_init: you ask for debug!

Nov 22 22:08:40 ubuntu kernel: [ 1680.874362] ville :ville_init: ville module init!

Nov 22 22:08:44 ubuntu kernel: [ 1685.649839] ville :ville_exit: you ask for debug!

Nov 22 22:08:44 ubuntu kernel: [ 1685.649841] ville :ville_exit: ville module exit!

Finally:

最后,发现自己还是个C程序员而已。

哎,但是改变还是要继续,时代在前进,我们每一个老东西都不能落后啊:)哈哈哈哈哈哈哈哈哈哈哈哈

原文地址:https://www.cnblogs.com/woodzcl/p/7884697.html