Linux的makefile文件例子 [英] Linux makefile example

查看:265
本文介绍了Linux的makefile文件例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新手到Linux和我通过一个实例工作我的方式
http://www.linuxforu.com/2010/12/写作,你先Linux的驱动程序/
我使用Ubuntu 12.04和创建了一个叫做ofd.c的C code文件(见下文),并保存在我的〜/开发/ MyProgs / myHelloWorldLinuxModule / V2创建的目录。我还创建了一个Makefile文件(见下文),这是在同一目录下。结果
我希望看到当我输入做出同样的目录下生成一个.ko文件,但我得到的是这样一条信息:默认将做什么

我真的不明白makefile文件

我应该定义KERNELRELEASE地方,

什么是默认实际上做行了,这是否意味着开展做出内核目录和工作目录或者我应该把我的code的地方尤其如此。

没有 usr / src目录/ Linux,但一个/usr/src/linux-headers-3.8.0-29 ,所以我改变这一点,是正确的。 (好像没任何区别,虽然)。

任何帮助将大大AP preciated。

code:

  / * ofd.c  - 我们的第一个司机code * /
#包括LT&; Linux的/ - module.h中GT;
#包括LT&; Linux的/ version.h中>
#包括LT&;的Linux / kernel.h>静态INT __init ofd_init(无效)/ * *构造函数/
{
    printk的(KERN_INFONamaska​​r:OFD注册);
    返回0;
}静态无效__exit ofd_exit(无效)/ * *析构函数/
{
    printk的(KERN_INFO阿尔维达:OFD未注册);
}宏module_init(ofd_init);
宏module_exit(ofd_exit);MODULE_LICENSE(GPL);
MODULE_AUTHOR(阿尼尔·库马尔Pugalia< email_at_sarika-pugs_dot_com>);
MODULE_DESCRIPTION(我们的第一个驱动程序);`

Makefile的......

 #的Makefile  - 我们的第一个驱动器的生成文件#如果KERNELRELEASE定义,我们已经从调用
#内核构建的系统,可以使用它的语言。
ifneq($ {} KERNELRELEASE,)
    OBJ-M:= ofd.o
#否则,我们直接在命令行中调用。
#调用内核构建系统。
其他
    KERNEL_SOURCE:=在/ usr / src / linux目录
    PWD:= $(壳PWD)
默认:
    $ {MAKE} -C $ {} KERNEL_SOURCE SUBDIRS = $ {PWD}模块清洁:
    $ {MAKE} -C $ {} KERNEL_SOURCE SUBDIRS = $ {PWD}干净
万一


解决方案

该错误是由于命名约定你的makefile是不是能够找到源$ C ​​$ C
该内核通过键入运行首先检查的uname -a

然后到 CD / usr / src目录/

然后检查你的Linux源码 - code名称

对于例如

的uname -a
Linux的维奈 - VirtualBox虚拟3.2.0-50-仿制PAE#76 Ubuntu的SMP周二7月9日19时24分55秒UTC 2013 i686的的i686 i386的GNU / Linux的

在这里它的源 - code的名字叫 Linux的头-3.2.0-50 - 仿制PAE

因此​​,在您的Makefile u需要给予正确的名称是这样

KERNEL_SOURCE:= /usr/src/linux-headers-3.2.0-50-generic-pae

要避免上述问题试试这个Makefile

 #如果KERNELRELEASE定义,我们已经从调用
#内核构建的系统,可以使用它的语言。
ifneq($(KERNELRELEASE))
  OBJ-M:= ofd.o
#否则,我们都是直接从命令调用
#线;调用内核构建系统。
其他
    KERNELDIR?= / lib / modules目录/ $(壳使用uname -r)/建
    PWD:= $(壳PWD)
默认:
    $(MAKE)-C $(KERNELDIR)M = $(PWD)模块
万一

下面的uname -a可以解决该问题。

Hi I am a newbie to linux and am working my way through an example http://www.linuxforu.com/2010/12/writing-your-first-linux-driver/ I am using Ubuntu 12.04 and have created the c code file called ofd.c (see below) and is is saved in a directory I created at ~/Development/MyProgs/myHelloWorldLinuxModule/v2. I have also created a Makefile (see below) which is in the same directory.
I was hoping to see a .ko file generated in the same directory when I type make, but all I get is a message saying "Nothing to be done for default"

I don't really understand the makefile

should I define KERNELRELEASE somewhere,

what is the line at default actually doing,does this mean carry out make on the kernel directory and the working directory or am I supposed to put my code somewhere in particular.

there is no usr/src/linux but a /usr/src/linux-headers-3.8.0-29, so I changed this, is that correct. (Didn't seem to make any difference though).

Any help would be greatly appreciated.

Code:

/* ofd.c – Our First Driver code */
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>

static int __init ofd_init(void) /* Constructor */
{
    printk(KERN_INFO "Namaskar: ofd registered");
    return 0;
}

static void __exit ofd_exit(void) /* Destructor */
{
    printk(KERN_INFO "Alvida: ofd unregistered");
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("Our First Driver");`

Makefile......

# Makefile – makefile of our first driver

# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq (${KERNELRELEASE},)
    obj-m := ofd.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
else
    KERNEL_SOURCE := /usr/src/linux
    PWD := $(shell pwd)
default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif

解决方案

The error is due to naming convention your makefile is not able to find source code First check which kernel is running by typing uname -a

Then go to cd /usr/src/

then check your linux source-code name

for e.g

uname -a Linux vinay-VirtualBox 3.2.0-50-generic-pae #76-Ubuntu SMP Tue Jul 9 19:24:55 UTC 2013 i686 i686 i386 GNU/Linux

here its source-code name is linux-headers-3.2.0-50-generic-pae

So in your Makefile u need to give correct name like this

KERNEL_SOURCE := /usr/src/linux-headers-3.2.0-50-generic-pae

To avoid above problem try this Makefile

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
  obj-m := ofd.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

here uname -a resolves the problem

这篇关于Linux的makefile文件例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆