LinuxQuestions.org
Help answer threads with 0 replies.
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 03-21-2006, 02:21 AM   #1
venkatesh111
Member
 
Registered: Mar 2006
Posts: 34

Rep: Reputation: 15
why is my 1st module not compiling


hi
i have a simple module like this:


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


static int hello_init(void)
{
printk(KERN_ALERT "Hello, world 2\n");
return 0;
}


static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, world 2\n");
}


module_init(hello_init);
module_exit(hello_exit);


i tried to compile it like this:

gcc hello.c

but i recived error messages as fallows:

[root@localhost modules]# gcc hello.c
hello.c: In function `hello_init':
hello.c:11: error: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: error: (Each undeclared identifier is reported only once
hello.c:11: error: for each function it appears in.)
hello.c:11: error: syntax error before string constant
hello.c: In function `hello_exit':
hello.c:18: error: `KERN_ALERT' undeclared (first use in this function)
hello.c:18: error: syntax error before string constant
[root@localhost modules]#



plz guide me........

venki
 
Old 03-21-2006, 02:29 AM   #2
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,

This is kernel module compilation not a user program you need Makefile to compile your program.the header file u r using u need to provide a path of kernel source to gcc command so that you will not get any error of KERN_ALERT
 
Old 03-21-2006, 02:31 AM   #3
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
Forgot to post Makefile
Quote:
obj-m := hello.o

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

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
create this Makefile file in current directory where your hello.c located and then simply type make
 
Old 03-21-2006, 02:47 AM   #4
venkatesh111
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
sorry actually i forgot to give u the Makefile
i had written makefile which was like this

TARGET := hello
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc33

${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
rm -rf {TARGET}.o

i issued make command in the current directory wr my makefile n modules r present but error was reported as fallows:


[root@localhost modules]# make
gcc-3.3.2 -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include -c -o hello.o hello.c
/bin/sh: line 1: gcc-3.3.2: command not found
make: *** [hello.o] Error 127


n plz give me a compleate command what should be my gcc command to compile my module..........
 
Old 03-21-2006, 02:50 AM   #5
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
which kernel version r u using?
 
Old 03-21-2006, 02:54 AM   #6
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
change CC=gcc33 to CC=gcc in your Makefile and then try compiling again
 
Old 03-21-2006, 02:57 AM   #7
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
venki, you cannot just compile program using gcc command line (ok you can but you need to type long set of commands) insted you need to create a makefile. Then compile program; your program is fine and it can be compiled without problem. The procedure to create makefile and commands are outlined here http://www.cyberciti.biz/nixcraft/vi...nel-module.php
 
Old 03-21-2006, 03:10 AM   #8
venkatesh111
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
my kernel version is: 2.4.22

[root@localhost modules]# make
make: `hello.o' is up to date.
[root@localhost modules]#

thanks thats fine.......

now can i insert my module into kernel using insmod or is it required to
recompile my kernel before i insert my module...
 
Old 03-21-2006, 03:12 AM   #9
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
No need to recompile kernel code. Just use insmod hello.o as root
 
Old 03-21-2006, 03:31 AM   #10
venkatesh111
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
i issued the command
insmod hello.o
the result was:
[root@localhost modules]# insmod hello.o
Warning: loading hello.o will taint the kernel: no license
See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Module hello loaded, with warnings


then changed my hello.c to include the fallowing at the end of my c file

MODULE_LICENSE("GPL");
MODULE_AUTHOR("VENKATESH");

then it was fallowed by this:

[root@localhost modules]# gcc hello.c

errors reported were:

hello.c: In function `hello_init':
hello.c:11: error: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: error: (Each undeclared identifier is reported only once
hello.c:11: error: for each function it appears in.)
hello.c:11: error: syntax error before string constant
hello.c: In function `hello_exit':
hello.c:18: error: `KERN_ALERT' undeclared (first use in this function)
hello.c:18: error: syntax error before string constant


wt is the gcc command to include source to kernel dir...
my hello.c file is in /venki/modules/ and kernel path is /usr/src/linux-2.4.22

my friend was suggesting me to use -c option to compile is it ok to use -c option that is gcc -c hello.c can i use this command
 
Old 03-21-2006, 04:10 AM   #11
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
Whats output of following command
gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -I/usr/src/linux-2.4.22/include -c -o hello.o hello.c
 
Old 03-21-2006, 04:14 AM   #12
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
USe following Makefile as you r using 2.4 kernel
Quote:
TARGET := modulename
INCLUDE := -I/lib/modules/`uname -r`/build/include
CFLAGS := -O2 -Wall -DMODULE -D__KERNEL__ -DLINUX
CC := gcc

${TARGET}.o: ${TARGET}.c
$(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c
The one that i was posted before was for kernel 2.6
 
Old 03-21-2006, 04:53 AM   #13
venkatesh111
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
i used the gcc command with makefile u wrote
it goes as fallows:

[root@localhost modules]# gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -I/usr/src/linux-2.4/include -c -o hello.o hello.c
In file included from /usr/src/linux-2.4/include/linux/prefetch.h:13,
from /usr/src/linux-2.4/include/linux/list.h:6,
from /usr/src/linux-2.4/include/linux/module.h:12,
from hello.c:4:
/usr/src/linux-2.4/include/asm/processor.h:503: warning: no previous prototype for `prefetch'
In file included from /usr/src/linux-2.4/include/linux/kernel.h:15,
from hello.c:5:
/usr/src/linux-2.4/include/asm/byteorder.h:14: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4/include/asm/byteorder.h:30: warning: type qualifiers ignored on function return type
In file included from /usr/src/linux-2.4/include/linux/byteorder/little_endian.h:11,
from /usr/src/linux-2.4/include/asm/byteorder.h:65,
from /usr/src/linux-2.4/include/linux/kernel.h:15,
from hello.c:5:
/usr/src/linux-2.4/include/linux/byteorder/swab.h:160: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4/include/linux/byteorder/swab.h:173: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4/include/linux/byteorder/swab.h:186: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4/include/linux/byteorder/swab.h:200: warning: type qualifiers ignored on function return type
/usr/src/linux-2.4/include/asm/processor.h: In function `copy_segments':
/usr/src/linux-2.4/include/asm/processor.h:453: warning: unused parameter `p'
/usr/src/linux-2.4/include/asm/processor.h:453: warning: unused parameter `mm'
/usr/src/linux-2.4/include/asm/processor.h: In function `release_segments':
/usr/src/linux-2.4/include/asm/processor.h:454: warning: unused parameter `mm'
/usr/src/linux-2.4/include/linux/prefetch.h: In function `prefetchw':
/usr/src/linux-2.4/include/linux/prefetch.h:48: warning: unused parameter `x'
[root@localhost modules]#

Last edited by venkatesh111; 03-21-2006 at 05:00 AM.
 
Old 03-21-2006, 05:05 AM   #14
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
just insert a tab at line 7 in your Makefile and try again with make command
arghhh i forgot 2.4 kernel module compilation using gcc
now try simple command
gcc -I/lib/modules/`uname -r`/build/include -D__KERNEL__ -DMODULE -DLINUX -O2 -Wall -Wstrict-prototypes -c -o hello.o hello.c
 
Old 03-21-2006, 10:41 PM   #15
venkatesh111
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
Smile to parag

hey it worked man thanks..........
 
  


Reply



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
compiling a module eltn Programming 5 04-01-2006 07:30 PM
Error Compiling GCC (1st Pass) in LFS 6.0 TGWDNGHN Linux From Scratch 1 06-30-2005 08:57 PM
compiling orinoco module BashTin Linux - Wireless Networking 0 02-02-2005 03:29 AM
Kernel compiling and module compiling tarballed Linux - General 1 12-22-2002 05:31 PM
compiling the 8390.o module islandkid Linux - General 1 08-08-2002 04:55 AM

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

All times are GMT -5. The time now is 08:42 PM.

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