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 going to learn kernel programming based on LLD2 book.
there is a hello module example in the chapter 2 that i can't compile it with gcc.
here's the code:
You need to build the module within the Context of the Linux tree. By default, the compiler will look for user-space headers in /usr/include. There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly.
In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make:
Code:
obj-m += foo.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
This triggers the kernel build system.
Given that you are using Ubuntu, you probably already have the kernel headers installed at /usr/src/linux-headers-$(uname -r). module.h lives here:
Code:
jameson@aqua:~$ ls /usr/src/linux-headers-$(uname -r)/include/linux/module.h
/usr/src/linux-headers-3.0.0-12-generic/include/linux/module.h
Last edited by jhwilliams; 11-26-2011 at 04:39 AM.
You shouldn't be running gcc directly. That's the problem. Just run make. Also, that won't work at this point because you have an error in your makefile.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.