Linux makefile example

2019-08-10 19:44发布

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

标签: c linux makefile
4条回答
手持菜刀,她持情操
2楼-- · 2019-08-10 19:59

You have an error in your Makefile.

You must use a tab character on the lines defining the actions to take against the target, e.g. the lines:

default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

The line must have a tab from the start of the line to the ${MAKE}. You need to force a tab character there, otherwise the error you've witnessed will occur.

The Make syntax is very particular about this requirement, and it's an easy error to make. If you used an editor like vim, it would typically display the space characters to the user highlighted in red so that they can understand that an error is present there.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-10 20:01

The error is coming due to the reason, the path specified in Makefile for headers or kernel source code, for the label KERNEL_SOURCE is not able to find the headers, which you already mentioned you suspected. Instead of changing the path, /usr/src/linux-headers-3.8.0-29 to /usr/src/linux.

Change the path given in the Makefile as the actual version of kernel whose source code you have or if the same kernel you are running you can get the version by uname command.

The better approach could be :

default:
    $(MAKE) -C /usr/src/$(shell uname -r)/ M=$(shell pwd) modules
查看更多
走好不送
4楼-- · 2019-08-10 20:02

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

查看更多
我只想做你的唯一
5楼-- · 2019-08-10 20:09

You didn't show the make command you ran to get this error. However, an error like that usually means that you have a file or subdirectory named default in your directory. Make wants to build targets and the first target in the makefile is default. It has no defined prerequisites which means that if it exists, make assumes it's up-to-date and nothing needs to be done to build it (it exists, and it doesn't depend on anything else).

You should probably use this:

.PHONY: default clean

to declare the default and clean targets to be "phony"; this tells make that even if those targets exist already it should still always run the recipe associated with them.

查看更多
登录 后发表回答