linux下增加新源码到 linux内核或 模块

GUN autotools工具使用步骤

步骤1:编写源码文件。

[root@localhost home]# vi hello.c

内容如下:

#include"hello.h"

int main()

{

printf("Hello world!\n");

}

[root@localhost home]# vi hello.h

内容如下:

#include<stdio.h>

步骤2:在当前目录下建立auto目录。

命令:

[root@localhost home]# mkdir auto

步骤3:把hello文件复制到auto目录下。

命令:

[root@localhost home]# cp hello.* ./auto

步骤4:输入命令autoscan。

命令:

[root@localhost home]# cd auto

[root@localhost auto]# autoscan

此时生成了2个文件 ,分别是 autoscan.log configure.scan

Autoscan会在给定的目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。它会搜索源文件以寻找一般的移植性问题并创建一个文件configure.scan

步骤5:用vi编写configure.scan,并保存命名为configure.in,configure.in是autoconf的脚本配置文件。

命令:

[root@localhost auto]# vi configure.scan

修改内容如下:

# -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.57)

AC_INIT(hello,1.0)

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AM_INIT_AUTOMAKE
AC_PROG_RANLIB  这两行需要手动添加

AC_CONFIG_SRCDIR([hello.c])

AM_CONFIG_HEADER([config.h])

# Checks for programs.

AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])--------------------指定生成Makefile。如果没有该行,请手动添加。

AC_OUTPUT下面对这个脚本文件进行解释:

AC_PREREQ宏声明本文件要求的autoconf版本,本例使用的版本为2.57

AC_INIT宏用来定义软件的名称和版本等信息

AM_INIT_AUTOMAKE是笔者另加的,它是automake所必备的宏。

AC_CONFIG_SRCDIR宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性,在此处为当前目录下的hello.c

AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader使用。

步骤6:运行命令aclocal

命令:

[root@localhost auto]# aclocal

生成文件aclocal.m4,该文件主要处理本地的宏定义。

步骤7:运行autoconf

命令:

[root@localhost auto]# autoconf

生成2个文件:分别为autom4te.cache configure

步骤8:运行autoheader

命令:

[root@localhost auto]# autoheader

生成config.h.in

步骤9:用vi编辑Makefile.am

[root@localhost auto]# vi Makefile.am

内容如下:

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=hello

hello_SOURCES=hello.c hello.h

步骤10:运行automake

命令:

[root@localhost auto]# automake --add-missing

可以让automake自动添加一些必需的脚本文件。

生成configure depcomp install-sh missing mkinstalldirs

步骤11:运行./configure

命令:

[root@localhost auto]# ./configure

显示内容如下
[root@localhost auto]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for ranlib... ranlib
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile------------------------如果没有该行请检查configure.in文件是否存在AC_CONFIG_FILES([Makefile])
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands

步骤12:运行make

命令:

[root@localhost auto]# make

步骤13:执行./hello

命令:

[root@localhost auto]# ./hello

显示结果如下:

Hello world!

步骤14:运行make install

命令:

[root@localhost auto]# make install

把程序安装到系统目录下。

[root@localhost auto]# hello

Hello world!

步骤15:运行make dist

命令:

[root@localhost auto]# make dist

打包

步骤16:运行make clean

命令:

[root@localhost auto]# make clean

显示如下:

test -z "hello" || rm -f hello

rm -f *.o core *.core

此时.o文件及可执行文件都会删除,也就是说hello及hello.o文件都删除了。

 

以上的的编译文件是属于上层的编译条件,不能作为内核模块的编译,也就是编译成.O文件,不能再源码中使用内核函数。一般对驱动没作用。

 

另外一种编译方式:  属于内核级的编译,编译成.KO文件

1. 把test.c文件放到YLP2440的根目录下,(和板子里用的内核相同的内核版本),YLP2440是这个内核再PC中的文件目录2. 然后修改YLP2440根目录的Makefile文件,在头部添加如下代码:

    obj-m := test.o             //选择编译成内核模块

    KERNELDIR := /root/test/YLP2440              //你的内核绝对路径

    PWD := $(shell pwd)        

    modules:

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

    modules_install:

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

    3. make 4. make结束后生成test.ko文件,然后把它放到文件系统中,新做一个cramfs文件系统,下载到板子里5. 启动板子,然后在test.ko文件目录下输入:insmod test.ko 6. 完成

    上述代码中,modules和modules_install为什么要取这个名字我也不知道,另一种代码写法是这样的:

    obj-m := test.o

    KERNELDIR := /root/test/YLP2440

    PWD := $(shell pwd)

    default:

    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

 

还有另外一种方式,就是参照DRIVER中的KCONFG  和   Makefile 来增加编译条件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值