一般内核驱动都是在实体机上跑的,那有没有方法在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. sql with multiply where

    I am wondering if this is a valid query: UPDATE table SET ID = 111111259 WHERE ID = 2555 AND SET ID ...

  2. shell 文件夹总大小 du -sh 文件夹

    du -sh 文件夹 du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件> ...

  3. [Lydsy1710月赛] 小B的数字

    神TM 又又又又是构造题..... 很简单的化简就是,把2^k[i]都换成k[i] ,然后就可以得出 对于任意的i,k[i] * a[i] >= ∑k[]. 最优的构造肯定是使  k[i] = ...

  4. (转)Unity3d通过Action注册事件,回调方法

    http://www.cnblogs.com/jisi5789/archive/2013/04/22/3036589.html using UnityEngine; namespace Liulala ...

  5. Word中更新交叉引用

    方法一:选择要更新的域,按F9键即可. 方法二:右键单击要更新的域,在弹出的右键菜单中选择“更新域”即可. 方法三:若域位于一个含有“更新”按钮的特定容器中,则点击“更新”即可.

  6. react数组key的唯一性

    1.不要使用数组的index索引作为key 2.在相邻的元素间,一定确保key的唯一性,如果出现了相同的 key,会抛出一个 Warning,告诉相邻组件间有重复的 key 值.并且只会渲染第一个重复 ...

  7. Console-算法:fun1(do while)

    ylbtech-Arithmetic:Console-算法[do while]-XX   1.A,Demo(案例) 1.B,Solution(解决方案) using System; namespace ...

  8. How to initialize th rasp berry PI

    WHAT YOU WILL NEED REQUIRED SD Card We recommend an 8GB class 4 SD card – ideally preinstalled with ...

  9. 【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print

    原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-depende ...

  10. Apache环境下搭建KodExplorer网盘

    Apache环境下搭建KodExplorer网盘 环境说明: 系统版本    CentOS 6.9 x86_64 软件版本    yum安装httpd和php    kodexplorer4.25 1 ...