Linux驱动模块编译模板

hello.c文件:

#include <linux/module.h>
#include <linux/kernel.h>

static int hello_init(void)
{
    printk("Hello,world!\n");

    return 0;
}

static void hello_exit(void)
{
    printk("Goodbye,cruel world!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("Vedic <FZKmxcz@163.com>");
MODULE_LICENSE("Dual BSD/GPL");

 

Makefile文件:

obj-m +=template.o
template-objs:= hello.o

KDIR:=/home/fuzk/project/Prolin/firmware_4/build_dir/linux-sc9820_sc9820_pax/linux-3.10.65

COMPILER=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-gcc
LD_PATH=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-ld
AR_PATH=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-ar
ARCH_TYPE=arm

#CCFLAGS新GGC要求改用ccflags-y, -I头文件必须是绝对路径 像-I./include压根找不到 宏定义用-D紧跟着宏名字即可不用空格 #ccflags
-y += -mfloat-abi=softfp -Ixx/xx/include -DXXX_RRR #ldflags-y += -L/opt/toolchain/arm-2012.03/lib/gcc/arm-none-linux-gnueabi/4.6.3 -lgcc -static all: make CC=$(COMPILER) LD=$(LD_PATH) AR=$(AR_PATH) ARCH=$(ARCH_TYPE) -C $(KDIR) M=$(PWD) modules clean: make CC=$(COMPILER) LD=$(LD_PATH) ARCH=$(ARCH_TYPE) -C $(KDIR) M=$(PWD) clean

 

追加:

  上面的Makefile其实写得不严谨!首先make命令后处理的Makefile是kernel下的Makefile, 查看内容得知里面除了用CC LD AR 还会用其他的

 

所以我们要做的是赋值CROSS_COMPILE变量才对

obj-m +=template.o
template-objs:= hello.o

KDIR:=/home/fuzk/project/Prolin/firmware_4/build_dir/linux-sc9820_sc9820_pax/linux-3.10.65

COMPILE=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-
ARCH_TYPE=arm

#CCFLAGS新GGC要求改用ccflags-y, -I头文件必须是绝对路径  像-I./include压根找不到   宏定义用-D紧跟着宏名字即可不用空格
#ccflags-y += -mfloat-abi=softfp -Ixx/xx/include -DXXX_RRR
#ldflags-y += -L/opt/toolchain/arm-2012.03/lib/gcc/arm-none-linux-gnueabi/4.6.3 -lgcc -static

all:
    make CROSS_COMPILE=$(COMPILE) ARCH=$(ARCH_TYPE) -C $(KDIR)  M=$(PWD)  modules

clean:
    make CROSS_COMPILE=$(COMPILE) ARCH=$(ARCH_TYPE) -C $(KDIR)  M=$(PWD)  clean

 

转载于:https://www.cnblogs.com/vedic/p/10622835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Linux字符设备驱动程序模板,供参考: ```c #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/uaccess.h> #define DEVICE_NAME "mychardev" #define CLASS_NAME "mycharclass" static int major_num = 0; static struct class* mycharclass = NULL; static struct device* mychardev = NULL; static struct cdev mycdev; static int mychardev_open(struct inode*, struct file*); static int mychardev_release(struct inode*, struct file*); static ssize_t mychardev_read(struct file*, char*, size_t, loff_t*); static ssize_t mychardev_write(struct file*, const char*, size_t, loff_t*); static struct file_operations mychardev_fops = { .owner = THIS_MODULE, .open = mychardev_open, .release = mychardev_release, .read = mychardev_read, .write = mychardev_write, }; static int __init mychardev_init(void) { // allocate major number dynamically if (alloc_chrdev_region(&major_num, 0, 1, DEVICE_NAME) < 0) { return -1; } // create device class if ((mycharclass = class_create(THIS_MODULE, CLASS_NAME)) == NULL) { unregister_chrdev_region(major_num, 1); return -1; } // create device if ((mychardev = device_create(mycharclass, NULL, major_num, NULL, DEVICE_NAME)) == NULL) { class_destroy(mycharclass); unregister_chrdev_region(major_num, 1); return -1; } // initialize cdev cdev_init(&mycdev, &mychardev_fops); mycdev.owner = THIS_MODULE; // add cdev to kernel if (cdev_add(&mycdev, major_num, 1) < 0) { device_destroy(mycharclass, major_num); class_destroy(mycharclass); unregister_chrdev_region(major_num, 1); return -1; } printk(KERN_INFO "mychardev: registered\n"); return 0; } static void __exit mychardev_exit(void) { cdev_del(&mycdev); device_destroy(mycharclass, major_num); class_destroy(mycharclass); unregister_chrdev_region(major_num, 1); printk(KERN_INFO "mychardev: unregistered\n"); } static int mychardev_open(struct inode* inodep, struct file* filep) { printk(KERN_INFO "mychardev: opened\n"); return 0; } static int mychardev_release(struct inode* inodep, struct file* filep) { printk(KERN_INFO "mychardev: closed\n"); return 0; } static ssize_t mychardev_read(struct file* filep, char* buffer, size_t len, loff_t* offset) { printk(KERN_INFO "mychardev: read from device\n"); return 0; } static ssize_t mychardev_write(struct file* filep, const char* buffer, size_t len, loff_t* offset) { printk(KERN_INFO "mychardev: wrote to device\n"); return len; } module_init(mychardev_init); module_exit(mychardev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple character device driver"); ``` 需要注意的是,该模板仅提供了一个最基本的字符设备驱动程序框架,需要根据实际需求进行修改。此外,还需要在Makefile中添加相应的编译规则,将驱动程序编译成内核模块。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值