Linux Development

This forum is for discussions about science, engineering, and technical subjects.
Site Admin
User avatar
Posts: 5937
Joined: Sat Oct 25, 2003 11:12 pm
Location: Gilbert, AZ

Linux Development

New postby Bud » Wed Jan 04, 2006 2:31 pm

"Linux Development" is a REALLY big topic. So don't look here expecting to find a tutorial. I will use this thread to keep notes and references so that I can easily find them later. This is a reference thread, so please don't post questions here. However, clarifications, corrections, and elaborations are always welcome (although, they may be integrated with the reference text and deleted.)

Books on Linux/Linux Development:

(Ref) [ISBN] <title>, by <author>
(1) [1-56592-585-8] Linux in a Nutshell: 2nd Edition, by Ellen Siever (and staff)
(2) [0-7645-4497-7] Beginning Linux Programming: 3rd Edition, by Neil Matthew and Richard Stones
(3) [1-56592-163-1] UNIX Systems Programming for SVR4, by David A. Curry
(4) [0-596-00213-0] Understanding the Linux Kernel: 2nd Edition, by Daniel P. Bovet & Marco Cesati
(5) [0-596-00590-3] Linux Device Drivers: 3rd Edition, by Alessandro Rubini, Jonathan Corbet & Greg Kroah-Hartman

Websites and Online Books:

(1.w) The Linux Kernel Module Programming Guide
(2.w) Kernel Newbies org
(4.w) Kernel Doc's Index
Last edited by Bud on Fri Mar 17, 2006 1:40 pm, edited 8 times in total.

Site Admin
User avatar
Posts: 5937
Joined: Sat Oct 25, 2003 11:12 pm
Location: Gilbert, AZ

Setting up a development system

New postby Bud » Wed Jan 04, 2006 3:06 pm

Linux Distributions:
Red Hat:

Kernel Makefile Documentation

1. Install Linux & set up system (per distributor'sinstructions).
2. Install GCC from native package manager or:
    a. Download GCC <dist>.tar.gz file from a mirror.
    b. Unzip files ("gunzip -d <dist>.tar.gz" followed by "tar -xf <dist>.tar").
    c. configure GCC (from within uzipped dir, give "mkdir <objdir>", "cd <objdir>", and "../configure [options] [target]" (see GCC install docs for details).
Last edited by Bud on Thu Feb 02, 2006 5:13 pm, edited 1 time in total.

Site Admin
User avatar
Posts: 5937
Joined: Sat Oct 25, 2003 11:12 pm
Location: Gilbert, AZ

Simple "Hello World" application.

New postby Bud » Fri Jan 06, 2006 2:05 pm

Use your favorite text editor and create the following text in a file named "hello.c".
Code: Select all
#include <stdio.h>

int main (int argc, char **argv)
        printf("Hello World!\n");

        return 0;

Compile using GCC.
gcc hello.c -o hello

Then execute it.

Giving output.
Hello World!

Reference: (2)

Site Admin
User avatar
Posts: 5937
Joined: Sat Oct 25, 2003 11:12 pm
Location: Gilbert, AZ

"Hello World" Module

New postby Bud » Sun Jan 08, 2006 6:29 pm

This note applies to Linux kernel version 2.6.

To build a Linux kernel module (which is how Linux device drivers are implemented), requires the following basic steps.

1. Install the kernel sources (should install into /usr/src/linux, a symlink to location for the version installed).
2. Configure the Linux kernel (use "make cloneconfig" to clone current configuration).
3. Build the installed Linux kernel* (use "make").
4. Implement & build the module (see example, below)
5. Install & debug the module.

*This is necessary starting w/Kernel 2.6 because some kernel binaries are linked w/the module. Follow your distributor's instructions, usually available in the /usr/src/linux/documentation directory.

Here's an example using SuSe Linux 10.0 and a simple "hello world" module.

Configuring and building the kernel after the installing it w/Yast.
cd /usr/src/linux
make oldcloneconfig

The build will may take quite a while to complete, depending on your configuration and the speed of the machine.

Here's a simple "hello world" module.
Code: Select all
#include <linux/init.h>
#include <linux/module.h>


static int hello_init(void)
    printk(KERN_ALERT "Hello world\n");

    return 0;

static  void hello_exit ( void )
    printk( KERN_ALERT "Goodbye world!\n" );


Here's an example "Makefile" file that should work with the new kbuild method used in kernel v2.6.
Code: Select all
# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
        obj-m := modhello.o

# Otherwise we were called directly from the command line;
# invoke the kernel build system.
        KERNELDIR ?= /lib/modules/$(shell uname -r)/build
        PWD := $(shell pwd)

        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules


Note: If you are not building the kernel in the default location you may have to modify the KERNELDIR macro.

Once both the source file (let's call it "modhello.c" in this example) and make file (called "Makefile") have been created, here's an example of building, inserting, and removing the module.

insmod ./modhello.ko
rmmod modhello

Sample make output
make -C /lib/modules/2.6.13-15-smp/build M=/home/Bud/development/modules/modhello modules
make[1]: Entering directory `/usr/src/linux-2.6.13-15'
Building modules, stage 2.
make[1]: Leaving directory `/usr/src/linux-2.6.13-15'

Sample output from the insmod command
Hello world

Sample output from the rmmod command
Goodbye world

These messages should match the ones included in the "printk" parameters in the source code. They may either appear at the terminal or in the /var/log/messages" file, depending on the system settings and the type of terminal at which you enter the commands.
Last edited by Bud on Mon Feb 13, 2006 10:59 am, edited 1 time in total.

Site Admin
User avatar
Posts: 5937
Joined: Sat Oct 25, 2003 11:12 pm
Location: Gilbert, AZ

Parsing Command Line parameters

New postby Bud » Fri Feb 10, 2006 3:09 pm

Linux provides getopt() support to support X/Open standard command line option usage.

Code: Select all
// Include this header
#include <unistd.h>

// getopt function prototype
int getopt ( int argc, char * const argv[], const char *optstring );

// Externs defined in the include
extern char *optarg;  // pointer to current option's argument (if needed).
extern int optind;  // Current option index
extern int opterr;  // Error code for current option.
extern int optopt;  // Current option value

The following sample code demonstrates the usage of getopt() and can be used as a basis for most any command line utility.

Source file:

Build Instructions:
gcc -o cmds cmds.c

Ref (2), cmds.c is my own code, but it is very similar to sample code in the book.

Site Admin
User avatar
Posts: 5937
Joined: Sat Oct 25, 2003 11:12 pm
Location: Gilbert, AZ

Misc Kernel 2.6 Info

New postby Bud » Fri Mar 17, 2006 5:20 pm

Module entry point called from "kernel\module.c" by a function named "sys_init_module" through a pointer from this code:
Code: Select all
   /* Start the module */
   if (mod->init != NULL)
      ret = mod->init();
   if (ret < 0) {

Device-specific, top-half IRQ handler called from "kernel\irq\handle.c" by a function named "handle_IRQ_event"
Code: Select all
   do {
      ret = action->handler(irq, action->dev_id, regs);
      if (ret == IRQ_HANDLED)
         status |= action->flags;
      retval |= ret;
      action = action->next;
   } while (action);

Return to Engineering

Who is online

Users browsing this forum: No registered users and 1 guest