一般内核驱动都是在实体机上跑的,那有没有方法在ubuntu直接编译并运行呢?带着这个问题在网上查了一些资料,之后就实现了。
运行
hello.c
 
  1. #include<linux/init.h>  //初始换函数
  2. #include<linux/kernel.h>  //内核头文件
  3. #include<linux/module.h>  //模块的头文件
  4.  
  5. static  int  hello_init(void)
  6. {
  7.     printk("aldrich Hello, world\n");
  8.     return0;
  9. }
  10.  
  11. staticvoid hello_exit(void)
  12. {
  13.     printk("aldrich Goodbye,Hello world\n");
  14. }
  15.  
  16. module_init(hello_init);
  17. module_exit(hello_exit);
  18. MODULE_LICENSE("BSD/GPL");
Makefile
 
  1. obj-m:=hello.o
  2. KERNELDIR:=/lib/modules/`uname -r`/build
  3. PWD :=$(shell pwd)
  4. modules:
  5. $(MAKE)  -C  $(KERNELDIR)  M=$(PWD)  modules
  6. clean:
  7. rm -rf *o *.mod.c *.order *.symvers
开始编译
 
  1. sudo make
输出
  1. make -C /lib/modules/`uname -r`/build M=/home/aldrich/work/hello modules
  2. make[1]:Entering directory '/usr/src/linux-headers-4.4.0-31-generic'
  3. CC [M]/home/aldrich/work/hello/hello.o
  4. Building modules, stage 2.
  5. MODPOST 1 modules
  6. CC /home/aldrich/work/hello/hello.mod.o
  7. LD [M]/home/aldrich/work/hello/hello.ko
  8. make[1]:Leaving directory '/usr/src/linux-headers-4.4.0-31-generic'
生成的文件
  1. hello.c hello.mod.c hello.o modules.order
  2. hello.ko hello.mod.o MakefileModule.symvers
加载模块
 
  1. sudo insmod hello.ko
会在/var/log/syslog文件中输出该打印信息
查看加载模块
 
  1. sudo  lsmod
.
 
  1. Module                 Size  Used by
  2. hello                  16384  0
删除模块
 
  1. sudo  rmmod  hello
查看输出的打印信息‘
 
  1. cat  /var/log/syslog  |grep  world
.
 
  1. Feb2019:33:17 tom kernel:[3792.439223] aldrich Hello, world
  2. Feb2019:33:50 tom kernel:[3824.708500] aldrich Goodbye,Hello world
至此。
 
 
 
 
 
 
 
 
 
 

Ubuntu下编译Hello World驱动并运行全过程的更多相关文章

  1. Ubuntu下编译C++ OpenCV程序并运行

            因为想试跑yolov3的缘故,所以装了ubuntu系统,直接通过U盘装的,并不像他们说的“折腾”,反而一切非常顺利,比装软件还简单.然后就是要用C++跑opencv的程序用于比赛,出于 ...

  2. 【转】在Ubuntu下编译Android源码并运行Emulator

    原文网址:http://www.mcuos.com/thread-4553-1-1.html 建立编译环境 1.在VirtualBox上安装Ubuntu 2.安装JDK   $ sudo apt-ge ...

  3. ubuntu下编译VLC

    ubuntu下编译VLC 标签(空格分隔): ubuntu vlc 视频 编译 [TOC] 1.下载VLC源码包并解压 VLC的源码包在VLC的官网有,可以直接下载.也可以使用git来clone一个. ...

  4. Ubuntu下安装nvidia显卡驱动

    layout: post title: Ubuntu下安装nvidia显卡驱动 date: 2015-10-02 17:19:06 categories: 常用命令 tags: 显卡 驱动 最近一直在 ...

  5. 在Ubuntu下编译Assimp库

    在Ubuntu下编译Assimp库 怎样在Ubuntu下编译Assimp库?这是我曾经编译成功后做的笔记,供參考. 1.去以下的站点去下载Assimp库: http://assimp.sourcefo ...

  6. ubuntu下编译java程序

    ubuntu下编译java程序 首先需要安装jdk,并配置好相应环境变量 下面以简单的HelloWorld为例 文件名为HelloWorld.java java代码: public class Hel ...

  7. ubuntu下编译VLC源码

    http://blog.csdn.net/beitiandijun/article/details/9225591ubuntu下编译VLC源码 分类: 视频处理 2013-07-02 17:33 57 ...

  8. ubuntu下编译caffe

    Ubuntu下编译caffe 纯粹是个人编译的记录.不用CUDA(笔记本是amd卡,万恶的nvidia):不手动编译依赖包(apt-get是用来干啥的?用来直接装二进制包,以及自动解决依赖项的) ca ...

  9. 在Ubuntu下编译wpa_supplicant

    最近在研究WiFi联盟的多屏互动协议Miracast,其中需要用到wpa_supplicant,编译中遇到了一些问题,经过一下午的折腾也都解决了,下面分享给各位. 一.编译需要用到三个库源码包,分别是 ...

随机推荐

  1. C++ 二位数组做参数传递

    指针的强大功能,,,,简直牛逼!!! #include<iostream> #include<cstdio> #include<map> using namespa ...

  2. 洛谷——P1469 找筷子

    P1469 找筷子 题目描述 经过一段时间的紧张筹备,电脑小组的“RP餐厅”终于开业了,这天,经理LXC接到了一个定餐大单,可把大家乐坏了!员工们齐心协力按要求准备好了套餐正准备派送时,突然碰到一个棘 ...

  3. BZOJ 2669- [cqoi2012]局部极小值

    不错的题啊 挺好的结合了容斥和状压DP 保证每个数各不相同,又有大小关系,那么就可以将数字从小到大填. 不难发现 局部极小值<=8,这个可以状压,f[i][j] 表示填了前i个数,局部极小值被填 ...

  4. Shader与AGAL(From 7yue)

  5. symbol(s) not found for architecture arm64

    问题如下:       解决:更改环境 ok   Standard architectures (armv7, arm7s)

  6. Delphi Modbus RTU CRC16校验码

    function CheckCrc16(const ABuf; ALen: Integer): Boolean;var uwTemp: WORD; i, j: BYTE; P: PByte;begin ...

  7. [Android Traffic] 让android应用在传输网络数据的时候更省电

    到今年6月,我国的手机网民已经达到了3.88亿,超过了电脑终端.相信有智能机的同学都用过手机上网冲浪.但是手机的电量很快被用光了恐怕是每个人都不能忍受的一件事情.而打开数据连接进行网络数据的传输是很耗 ...

  8. hive参数——深入浅出学Hive

    第一部分:Hive 参数 hive.exec.max.created.files •说明:所有hive运行的map与reduce任务可以产生的文件的和 •默认值:100000  hive.exec.d ...

  9. Unity Dotween build error

    unity这东西感觉挺坑 在mac上build的时候遇到error IL2CPP error for method 'System.Void DG.Tweening.DOTweenPath::DORe ...

  10. AngularJS: Dynamically loading directives

    http://www.codelord.net/2015/05/19/angularjs-dynamically-loading-directives/ ----------------------- ...