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.
I'm trying to build a very simple device driver using Mandrake 10 with the 2.6 kernel. Every attempt seems to fail and all the links I'm finding explain the procedure for the previous kernel versions. I found an example on a website that said this should work. Bellow is a cut and past directly from the website.
My source file "helllo2.c" looks like this.
/* hello2.c - Demonstrating the module_init() and module_exit() macros. This is the
* preferred over using init_module() and cleanup_module().
*/
#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_2_init(void)
{
printk(KERN_ALERT "Hello, world 2\n");
return 0;
}
static void hello_2_exit(void)
{
printk(KERN_ALERT "Goodbye, world 2\n");
}
Don't you get your kernel module helllo2.ko issuing this Makefile ? It need nothing after the comma, I grabbed this Makefile as this from the link I mentioned above (but it seems break today).
For me it's worked, I result with a .ko module which I loads from insmod modulename.ko, when I typed dmesg, it give me expected output messages ( printk(KERN_ALERT "Hello, world 2\n"); )
Can anybody help with the make file for a 2.4 kernel? I found the website you are using and I'd really like to try this on a 2.4 kernel. Once you write the make file and get the code compiled, how does it actually get added to the kernel?
Do I need to place my source file anywhere specific to get this to work? I wrote the make file mentioned above and ran "make hellokernel.c" and got the "Nothing to be done for hellokernel.c" message. Then I tried to compile it manually with gcc and got compile errors. It looks like the header files cannot be found.
AMDPwred> Adapt Makefile to suit your project by replacing target value by your module name.
ie for first Makefile:
TARGET := hellokernel
for second Makefile (provided by infamous41md) :
OBJECTS = hellokernel.o
And when invoking make do not use extension after module name. ie:
make hellokernel
Ok cool, that got the make piece running. During the compile I still get those errors. Do I need to include a path to these header files in my system PATH?
hellokernel.c
Code:
/*
* hellokernel.c
* Demonstrating the init_module() and cleanup_module().
*/
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello kernel!\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Peace out kernel!\n");
}
error during make
Code:
chris@zion kernelmodule $ make hellokernel
gcc hellokernel.c -o hellokernel
hellokernel.c: In function `init_module':
hellokernel.c:12: error: `KERN_INFO' undeclared (first use in this function)
hellokernel.c:12: error: (Each undeclared identifier is reported only once
hellokernel.c:12: error: for each function it appears in.)
hellokernel.c:12: error: syntax error before string constant
hellokernel.c: In function `cleanup_module':
hellokernel.c:18: error: `KERN_INFO' undeclared (first use in this function)
hellokernel.c:18: error: syntax error before string constant
make: *** [hellokernel] Error 1
Here is the error output after removing those two phrases:
Code:
chris@zion kernelmodule $ make hellokernel
gcc hellokernel.c -o hellokernel
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
/tmp/ccmDnTfs.o(.text+0xe): In function `init_module':
: undefined reference to `printk'
/tmp/ccmDnTfs.o(.text+0x27): In function `cleanup_module':
: undefined reference to `printk'
collect2: ld returned 1 exit status
make: *** [hellokernel] Error 1
verify in your /lib/modules/`uname -r`/build/include/linux directory that module.h and kernel.h exist, if yes verify in your Makefile that you have inclued the directory by parameter -isystem (which make sure that /lib/modules/`uname -r`/build/include/ will be used instead of /usr/include)
Those header files don't exist in that directory. Which is weird because I remember seeing them browsing the kernel source tree one day. I can't seem to find them at the moment though.
Update:
I found a directory where they do exist:
Code:
/usr/src/linux/include/linux
I used that in the make file and I'm getting the same error. I also double checked to make sure I'm using -isystem for the include path.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.