Writing a "Hello World" Device Driver for kernel 2.6 using Eclipse
Solution 1
I'm in almost same position with you. Following this istructions I have had success with building kernel itself, and a single module.
I added three steps (40~42) to main article to make Eclipse compile a specific driver, not whole kernel.
- Download and install Eclipse plus the CDT.
- Configure and build your kernel to define
CONFIG_*
and generateautoconf.h
. This can be done before or after downloading and installing Eclipse. - Ensure that you have the right kernel source (e.g. make sure you are on the right git branch). If you check out another branch later, that's ok, but you will need to re-index the source, and that takes about 20 minutes.
- Start up Eclipse.
- Click
File
->New
->C Project
- Fill in a project name like
my_kernel
- Uncheck the Use default location box and type in the root directory of your kernel into the Location box.
- In the Project type: pane, click the Makefile project and select Empty Project
- On the right side, select
Linux GCC
- Click
Advanced settings
... and a Properties dialog will pop up. - Select Resource on the left, and then in the Text file encoding section, select Other and
ISO-8859-1
in the box, then click Apply - Open the
C/C++ General
selection on the left. - Click on
Preprocessor Include Paths
- Select
GNU C
in the Languages list - Select
CDT User Setting Entries
in the Setting Entries list - Click on
Add
.... Choose Preprocessor Macros File from the top left dropdown, Project Path from the top right dropdown, and enterinclude/generated/autoconf.h
into the File text box. (Note: for older kernels [pre-2.6.36?], the location ofautoconf.h
isinclude/linux/autoconf.h
) - Also add any other macros files you are using.
- Click on Indexer
- Checkmark the Enable project specific setttings box.
- Uncheck Index source files not included in the build
- Clear out the Files to index up-front box.
- Click on Paths and Symbols on the left.
- Select the Includes tab and then select
GNU C
- Click
Add
... - Click
Workspace
... then select your kernel's include directory - Do another Add, Workspace and add
arch/architecture/include
, e.g.,arch/powerpc/include
- Click the
# Symbols
tab - Click
Add
... - Set the name to
__KERNEL__
- Set the value to
1
and click OK - Click the Source Location tab
- Click the twisty for your project.
- Select the Filter item and click Edit Filter...
- Click Add Multiple... and then select all of the arch/* directories in your kernel source that will not be used (i.e. all the ones that are not for the architecture you are using)
- Click OK and OK again to dismiss that dialog.
- Click OK on the Properties dialog.
- Click Finish on the C Project dialog.
- Right click on the project and select Index then select Rebuild
- It will take about 20 minutes or so to complete.
- Open your project setting, go to the
C/C++ build -> Behaviour (tab)
- Check the
Build (Incremental buil)
checkbox and add your module path to the textbox (in my caseM=drivers/servo/dynamixel
). - When you're module is ready and you want to compile kernel, repeat 41 and replace
M=..
withall
.
Solution 2
If you want to do driver development with eclipse, you will have to do it the other way round.
You will need to catch up on automake, autogen, pkg-config and so on, create an autotools project and import it into eclipse. The eclipse-cdt should offer this otherwise you missed to install the 'autotools-plugin' (unsure about exact name, writing this from memory).
Just abandon the idea that eclipse-cdt could manage a decent Makefile, C isn't Java, unfortunately or thankfully.
Solution 3
I am new too in Linux driver programming, I found that there a new way to deploy kernel modules (which are not in the official Linux tree) called DKMS. The module will be installed as source and DKMS will take care of compiling it for each kernel. It means the Makefile for module will be written manually and it's source will be not included for autotools.
updated...
DKMS moved to http://en.community.dell.com/techcenter/os-applications/w/wiki/2463.linux-projects.aspx
This tutorials/quick introduction to DKMS (links from Dell project page): Linux Journal article , Power Solutions paper , Ottawa Linux Symposium paper
DKMS used for quick driver deploying. For example kernel modules using DKMS in my Ubuntu machine:
dkms status
bcmwl, 6.20.155.1+bdcom, 3.5.0-41-generic, x86_64: installed
bcmwl, 6.20.155.1+bdcom, 3.5.0-42-generic, x86_64: installed
bcmwl, 6.20.155.1+bdcom, 3.5.0-43-generic, x86_64: installed
nvidia, 313.26, 3.5.0-42-generic, x86_64: installed
nvidia, 313.26, 3.5.0-43-generic, x86_64: installed
vboxhost, 4.3.0, 3.5.0-42-generic, x86_64: installed
vboxhost, 4.3.0, 3.5.0-43-generic, x86_64: installed
This is the code I have written before, It could be helpful as DKMS Hello World. https://github.com/sneetsher/RTD-DM5408-Driver-Port-for-Linux
Solution 4
1> you can try with this command
"make" --> "/usr/bin/make"
/usr/bin/make -C /lib/modules/2.6.38-8-generic/build M=/home/isaac/workspace/TestDriver1
2> in directory store your hello word code,creat file name "Makefile"
obj-m := NameofyourHelloWold.o
KDIR := /lib/modules/2.6.38-8-generic/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
Related videos on Youtube
Comments
-
Isaac almost 2 years
Goal
I am trying to write a simple device driver on Ubuntu. I want to do this using Eclipse (or a better IDE that is suitable for driver programming). Here is the code:
#include <linux/module.h> static int __init hello_world( void ) { printk( "hello world!\n" ); return 0; } static void __exit goodbye_world( void ) { printk( "goodbye world!\n" ); } module_init( hello_world ); module_exit( goodbye_world );
My effort
After some research, I decided to use Eclipse CTD for developing the driver (while I am still not sure if it supports multi-threading debugging tools). So I:
- Installed Ubuntu 11.04 desktop x86 on a VMWare virtual machine,
- Installed
eclipse-cdt
and linux-headers-2.6.38-8 using Synaptic Package Manager, - Created a
C Project
namedTestDriver1
and copy-pasted above code to it, - Changed the default build command,
make
, to the following customized build command:
make -C /lib/modules/2.6.38-8-generic/build M=/home/isaac/workspace/TestDriver1
The problem
I get an error when I try to build this project using eclipse. Here is the log for the build:
**** Build of configuration Debug for project TestDriver1 ****
make -C /lib/modules/2.6.38-8-generic/build M=/home/isaac/workspace/TestDriver1 all
make: Entering directory '/usr/src/linux-headers-2.6.38-8-generic'
make: *** No rule to make target
vmlinux', needed by
all'. Stop.make: Leaving directory '/usr/src/linux-headers-2.6.38-8-generic'
Interestingly, I get no error when I use
shell
instead ofeclipse
to build this project. To useshell
, I just create aMakefile
containingobj-m += TestDriver1.o
and use the abovemake
command to build.So, something must be wrong with the eclipse
Makefile
. Maybe it is looking for thevmlinux
architecture (?) or something while current architecture is x86. Maybe it's because of VMWare?As I understood,
eclipse
creates the makefiles automatically and modifying it manually would cause errors in the future OR make managingmakefile
difficult.So, how can I compile this project on eclipse?
-
Isaac almost 13 yearsThanks. I will check the
autotools
plugin soon. Do you know any better IDE for driver programming? Do you think I (a lazy VS user who have no experience in linux) can managemakefile
? Ismakefile
s really manageable in big projects? -
shay.porteous almost 13 yearsI was a lazy VS user too and I must admit mastering the autotools has a steep learning curve but it's very worthwhile, not only for linux. Eclipse is one of the best IDEs around, but autotools projects are so diverse that no IDE can parse all the possibilities. Try geany if you want to do it right, try anjuta and codelite if you liked VS6 but always be proficient at command line compile.