LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2010, 07:19 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
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?
 
Old 11-24-2010, 08:04 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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
 
Old 11-24-2010, 09:17 PM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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
 
1 members found this post helpful.
Old 11-24-2010, 10:22 PM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,513

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
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/
..
 
Old 11-25-2010, 08:01 AM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
@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
 
Old 11-25-2010, 08:10 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by eantoranz View Post
@anishakaul: The tabs made it work. Thanks!
I am happy that I could help you
 
  


Reply

Tags
linux module, make, makefile



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to Compile "hello world" Kernel Module (please help!) antdengineer Programming 9 12-04-2010 04:07 AM
The errors of "The hello world module" shuge_guet Linux - Kernel 2 04-06-2010 10:13 AM
Printk won't "print" from probe() ::::A hello world module-pci device knockout_artist Linux - Kernel 1 08-14-2008 03:41 AM
Unable to compile "hello world" module on openSuse11 knockout_artist Linux - Kernel 6 08-12-2008 04:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration