LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Linux module "Hello world" Makefile is missing something (http://www.linuxquestions.org/questions/programming-9/linux-module-hello-world-makefile-is-missing-something-846387/)

eantoranz 11-24-2010 07:19 PM

Linux module "Hello world" Makefile is missing something
 
Hi!

I'm trying to run a "make" on a basic Makefile I created for a "Hello world" linux module, however if I run "make" or "make clean" it fails to work. I have to write more stuff in order to make it work (redundant stuff cause I define that stuff in the Makefile).

First, the Makefile:
Code:

obj-m := helloworld.o

KDIR=/lib/modules/$(shell uname -r)/build
PWD=$(shell pwd)

all:
    make -C $(KDIR) M=$(PWD) modules

clean:
    make -C $(KDIR) M=$(PWD) clean

Now, see what happens when I call make directly:
Code:

$ make
make: Nothing to be done for `all'.
$ make clean
make: Nothing to be done for `clean'.

I have to duplicate what's in the Makefile for it to work:
Code:

$ make -C /lib/modules/$( uname -r )/build M=$PWD
make: Entering directory `/usr/src/linux-headers-2.6.35-22-generic'
  LD      /home/antoranz/projects/modexp/module/built-in.o
  CC [M]  /home/antoranz/projects/modexp/module/helloworld.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/antoranz/projects/modexp/module/helloworld.mod.o
  LD [M]  /home/antoranz/projects/modexp/module/helloworld.ko
make: Leaving directory `/usr/src/linux-headers-2.6.35-22-generic'
$ make -C /lib/modules/$( uname -r )/build M=$PWD clean
make: Entering directory `/usr/src/linux-headers-2.6.35-22-generic'
  CLEAN  /home/antoranz/projects/modexp/module/.tmp_versions
  CLEAN  /home/antoranz/projects/modexp/module/Module.symvers /home/antoranz/projects/modexp/module/modules.order
make: Leaving directory `/usr/src/linux-headers-2.6.35-22-generic'

What am I doing wrong?

frieza 11-24-2010 08:04 PM

would you be invoking make from within a makefile? i would imagine you'de be invoking the compiler
here's an example makefile from Xcplay (i didn't write the makefile i just happened to have it on hand)
Code:

SHELL := /bin/sh

.SUFFIXES:
.SUFFIXES: .c .o

DESTDIR =

prefix = /usr/local
bindir = $(prefix)/bin
srcdir = .

INSTALL = install
INSTALL_PROGRAM = $(INSTALL)

PROGRAM := xcplay
VERSION := 0.3.6
DISTFILES := $(PROGRAM).c Changelog COPYING INSTALL Makefile mkinstalldirs
DISTFILES += README TODO
DISTNAME := $(PROGRAM)-$(VERSION)

CFLAGS = -Wall -O2
XCPLAY_CFLAGS := $(shell xmms-config --cflags)
XCPLAY_LDFLAGS := $(shell xmms-config --libs) -lncurses -lpthread

all: $(PROGRAM)

$(PROGRAM).o: $(PROGRAM).c
        $(CC) -o $@ -c $< $(CFLAGS) $(XCPLAY_CFLAGS)

$(PROGRAM): $(PROGRAM).o
        $(CC) -o $@ $(LDFLAGS) $(XCPLAY_LDFLAGS) $<

installdirs: mkinstalldirs
        $(srcdir)/mkinstalldirs $(DESTDIR)$(bindir)

install: all installdirs
        $(INSTALL_PROGRAM) xcplay $(DESTDIR)$(bindir)

dist: clean
        mkdir dist
        mkdir dist/$(DISTNAME)
        $(foreach file, $(DISTFILES), cp -pR $(file) dist/$(DISTNAME)/;)
        tar -czf $(DISTNAME).tar.gz -C dist $(DISTNAME)
        chmod 644 $(PROGRAM)-$(VERSION).tar.gz
        rm -rf dist

clean:
        rm -rf $(PROGRAM) dist *.o *.tar.gz


TheIndependentAquarius 11-24-2010 09:17 PM

Here is an example of a Makefile for a kernel module.

Now the important thing here to note is that the red dots shown below must be replaced by a TAB, replacing them by spaces will cause Makefile to malfunction.
Code:

obj-m += hello.o

all:
.......make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
.......make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


knudfl 11-24-2010 10:22 PM

With no file helloworld.c , the make command will reply :
" make: Nothing to be done for `all'. "

Example hello.c
http://www.cyberciti.biz/tips/compil...el-module.html
Code:

#include <linux/module.h>      /* Needed by all modules */
#include <linux/kernel.h>      /* Needed for KERN_INFO */
#include <linux/init.h>        /* Needed for the macros */

static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}

static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}

module_init(hello_start);
module_exit(hello_end);

And the simple Makefile
Code:


obj-m    := hello.o

KDIR    := /lib/modules/$(shell uname -r)/build
PWD    := $(shell pwd)

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

Now you will get a hello.ko created with 'make'.
http://www.captain.at/programming/kernel-2.6/
..

eantoranz 11-25-2010 08:01 AM

@anishakaul: The tabs made it work. Thanks!

Also replacing make for $(MAKE) looks very elegant:

Code:

obj-m := helloworld.o

KDIR=/lib/modules/$(shell uname -r)/build
PWD=$(shell pwd)

all:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
        $(MAKE) -C $(KDIR) M=$(PWD) clean


TheIndependentAquarius 11-25-2010 08:10 AM

Quote:

Originally Posted by eantoranz (Post 4170740)
@anishakaul: The tabs made it work. Thanks!

I am happy that I could help you :)


All times are GMT -5. The time now is 07:21 AM.