ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int init_module(){
printk("<1>Hello world\n");
return 0;
}
void cleanup_module(){
printk("<1>Goodbye\n");
}
MODULE_LICENSE("GPL");
First I use simple way to compile, 'gcc -c main.c'. I have main.o then i used 'insmod ./main.o' but the error appear and show below.
Quote:
insmod: error inserting './main.o': -1 Invalid module format
Due to the first way failed I move to second.
Second I use 'gcc -c -D__KERNEL__ -DMODULE main.c' and the error is show below.
Quote:
In file included from /usr/include/linux/module.h:10,
from main.c:2:
/usr/include/linux/config.h:5:2: #error Incorrectly using glibc headers for a kernel module
Next I search through google and find another way to compile.
I used Makefile to compile the source as follow.
Then I use 'make default' to compile and the errors are show below.
Some of the errors.
Quote:
...
...
include/linux/slab.h:93: error: `flags' undeclared (first use in this function)
include/linux/slab.h:94: error: invalid use of undefined type `struct cache_sizes'
include/linux/slab.h:95: error: invalid use of undefined type `struct cache_sizes'
include/linux/slab.h: At top level:
include/linux/slab.h:100: error: syntax error before "int"
include/linux/slab.h:100: warning: function declaration isn't a prototype
include/linux/slab.h:119: error: syntax error before "slab_reclaim_pages"
include/linux/slab.h:119: warning: type defaults to `int' in declaration of `slab_reclaim_pages'
include/linux/slab.h:119: warning: data definition has no type or storage class
In file included from include/linux/sched.h:31,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/percpu.h:7:24: asm/percpu.h: No such file or directory
In file included from include/linux/sched.h:31,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/percpu.h:43: error: syntax error before "size"
include/linux/percpu.h:44: warning: function declaration isn't a prototype
include/linux/percpu.h: In function `__alloc_percpu':
include/linux/percpu.h:45: error: `size' undeclared (first use in this function)
In file included from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/sched.h: At top level:
include/linux/sched.h:95: error: syntax error before "process_counts"
include/linux/sched.h:95: warning: type defaults to `int' in declaration of `DECLARE_PER_CPU'
include/linux/sched.h:95: warning: function declaration isn't a prototype
include/linux/sched.h:95: warning: data definition has no type or storage class
In file included from include/linux/sched.h:103,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/resource.h:22: error: field `ru_utime' has incomplete type
include/linux/resource.h:23: error: field `ru_stime' has incomplete type
In file included from include/linux/sched.h:103,
from include/linux/module.h:10,
from /root/Programming/C/Kernel/Helloworld/main.c:2:
include/linux/resource.h:71:26: asm/resource.h: No such file or directory
make[2]: *** [/root/Programming/C/Kernel/Helloworld/main.o] Interrupt
make[1]: *** [_module_/root/Programming/C/Kernel/Helloworld] Interrupt
...
...
include/linux/sched.h:227: error: storage size of `mmap_sem' isn't known
include/linux/sched.h:261: error: storage size of `default_kioctx' isn't known
include/linux/sched.h:268: error: storage size of `action' isn't known
include/linux/sched.h:286: error: storage size of `shared_pending' isn't known
include/linux/sched.h:390: error: storage size of `wall_to_prev' isn't known
include/linux/sched.h:521: error: storage size of `start_time' isn't known
include/linux/sched.h:532: error: storage size of `rlim' isn't known
include/linux/sched.h:540: error: storage size of `thread' isn't known
include/linux/sched.h:552: error: storage size of `pending' isn't known
include/linux/stat.h:68: error: storage size of `atime' isn't known
include/linux/stat.h:69: error: storage size of `mtime' isn't known
include/linux/stat.h:70: error: storage size of `ctime' isn't known
{standard input}: Assembler messages:
{standard input}:378: Error: symbol `seq' is already defined
{standard input}:426: Error: symbol `sem_nsems' is already defined
{standard input}:449: Error: symbol `lock' is already defined
{standard input}:1584: Error: symbol `files' is already defined
{standard input}:1770: Error: symbol `blocks' is already defined
make[2]: *** [/root/Programming/C/Kernel/Helloworld/main.o] Error 1
make[1]: *** [_module_/root/Programming/C/Kernel/Helloworld] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.9-8.tlc'
make: *** [default] Error 2
It seem like it cannot find the header or somethings.
In this case I test it with Linux TLE 7.0 (2.6.9), gcc 3.4.2 and the kernel with Loadable Module Enabled.
What is the problem i'm facing and How to solve it?
First off, is the kernel tree you’re using the actual build tree for your kernel? It seems to me that you are trying to compile in a clean tree (or perhaps you are pulling headers from /usr/include (e.g., files such as “/usr/include/linux/*.h” instead of “/usr/src/linux-`uname -r`/include/linux/*.h”). Here’s a somewhat saner Makefile:
For one thing, it doesn’t assume your kernel build tree resides in /usr/src/linux-`uname -r` (which is not a standard practice). It also uses the M parameter.
P.S., when posting code samples, use [code] and [/code] instead of [quote] and [/quote] (it preserves monospacing as well as tabs and spaces).
But i see symlink from source to build then I decide to create 'build' directory, perhap maybe it's require.
That’s odd… source should be a link to your kernel sourcetree and build should be a link to your kernel build tree (usually these are the same), but you should never have to edit these by hand. My advice would be first, to back up /lib/modules, then to go to what you think is your kernel source tree (/usr/src/linux-2.6.9-8.tlc), and type (as root):
Code:
# make modules
# make modules_install
# depmod -ae
Last edited by osor; 05-01-2007 at 03:31 PM.
Reason: typo and codepage issues
Ok it work now, you are my hero, thanks again.
But for this time, when I issue 'insmod ./main.ko' the kernel module is loaded properly (checked by lsmod) but it not print anything out as per code instruction. Also when I issue 'rmmod main' nothing print again.
//-------------------------------------------------------------------
// jiffies.c
//
// This simple module create a 'pseudo' file (named 'jiffies')
// in the system's '/proc' directory. Then a user can look at
// the current value of the kernel's global 'jiffies' variable
// just by typing the command:
//
// $ cat /proc/jiffies
//
// programmer: ALLAN CRUSE
// written on: 24 JAN 2003
// revised on: 07 SEP 2004 -- for Linux kernel version 2.6.5
//-------------------------------------------------------------------
#include <linux/module.h> // for init_module()
#include <linux/proc_fs.h> // for create_proc_read_entry()
#define SUCCESS 0
static char modname[] = "jiffies";
static int my_proc_read( char *buf, char **start, off_t off,
int count, int *eof, void *data )
{
return sprintf( buf, "jiffies = %lu\n", jiffies );
}
int init_module( void )
{
create_proc_read_entry( modname, 0, NULL, my_proc_read, NULL );
return SUCCESS;
}
void cleanup_module( void )
{
remove_proc_entry( modname, NULL );
}
MODULE_LICENSE("GPL");
Above example code is work. But I curious about Helloworld Module why can't it print out both Helloworld when 'insmod', and Goodbye when 'rmmod'.
Thanks again osor, this help me a lot and let me go to way of practicing once again.
but it not print anything out as per code instruction. Also when I issue 'rmmod main' nothing print again.
How did you check this? Are you waiting for it to print to console, or did you check the output of “dmesg”? Kernel messages are not always printed to console (depending on what message level your system is set at), and even when they are, it must be a tty console (not an xterm). So instead of waiting for a message to console, check the output of the “dmesg” command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.