WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Help with compile of simple kernel module/driver  (Read 5008 times)

Offline neowox

  • Newbie
  • *
  • Posts: 4
Help with compile of simple kernel module/driver
« on: October 13, 2015, 08:07:23 AM »
I'm new to Tiny Core and I'm having a bit of trouble compiling a test kernel module.

First, I installed the sources, which completed successfully.
tce-load -iw linux-kernel-sources-env.tcz

Then I ran the script in the description of the kernel sources, which executed successfully.
cliorx linux-kernel-sources-env.sh

I built this simple module/driver and saved it as 'hello.c'.
Code: [Select]
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL")

static int hello_init(void)
{
  printk(KERN_ALERT "Hello, world\n");
  return 0;
}

static void hello_exit(void)
{
  printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Then I run the below command and try to compile.
cc hello.c -o hello

I get the following error.
hello.c:1:24: fatal error: linux/init.h: No such file or directory
#include <linux/init.h>
                                   ^

I also try to build the Makefile below.
Code: [Select]
obj-m += hello.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

Then I run the following command.
make

I get the following error from the 'make' command.

make -C /lib/modules/3.16.6-tinycore/build M=/home/tc modules
make[1]: Entering directory '/usr/src/linux-3.16.6'
Makefile:358: scripts/Kbuild.include: No such file or directory
Makefile:614: arch/x86/Makefile: No such file or directory
/bin/bash: ./scripts/gcc-goto.sh: No such file or directory
Makefile:755: scripts/Makefile.extrawarn: No such file or directory
make[1]: *** No rule to make target 'scripts/Makefile.extrawarn'. Stop.
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2


Thanks for your help!

« Last Edit: October 13, 2015, 08:17:53 AM by neowox »

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254

Offline neowox

  • Newbie
  • *
  • Posts: 4
Re: Help with compile of simple kernel module/driver
« Reply #2 on: October 13, 2015, 08:40:38 AM »
http://wiki.tinycorelinux.net/wiki:custom_kernel

Gerald_Clark; this link seems to be about building a custom kernel, not compiling kernel modules. Am I missing something? Thanks.
« Last Edit: October 13, 2015, 08:43:27 AM by neowox »

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Help with compile of simple kernel module/driver
« Reply #3 on: October 13, 2015, 09:19:03 AM »
That builds the kernel and its modules.
It sounds like you have not properly setup the build environment.

Offline neowox

  • Newbie
  • *
  • Posts: 4
Re: Help with compile of simple kernel module/driver
« Reply #4 on: October 13, 2015, 09:40:04 AM »
That builds the kernel and its modules.
It sounds like you have not properly setup the build environment.

I have the following packages installed in addition to linux-kernel-sources-env.tcz. I believe this is all I need for a build environment. Any ideas what additional steps are necessary to create a Tiny Core build environment? Any documentation? Thanks.

compiletc.tcz
linux-headers-3.0.21-tinycore.tcz
squashfs-tools-4.x.tcz
glibc_apps.tcz

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: Help with compile of simple kernel module/driver
« Reply #5 on: October 13, 2015, 09:46:24 AM »

Offline neowox

  • Newbie
  • *
  • Posts: 4
Re: Help with compile of simple kernel module/driver
« Reply #6 on: October 13, 2015, 02:09:03 PM »
Thanks Juanito, but that's not what I was looking for either.

I started from scratch and the Makefile worked. I'm not sure what I did the first few times to cause it to error out.

Perhaps it will help someone in the future, so here's what I did:

1. Create Build Environment
Run the commands:
tce-load -iw linux-kernel-sources-env.tcz
cliorx linux-kernel-sources-env.sh

2. Create Module
Create the simple module below from original post and save with the file name hello.c

3. Create Makefile
Create the Makefile from original post and save with the name Makefile

4. Compile source
Run the command, make  while in the same directory as your source hello.c and Makefile
This will create your compiled module.

5. Load the module
Run the command:
sudo insmod hello.ko
You should get the Hello, world message as a kernel alert in your prompt.

6. Check on the module
Type the following command to see if your module is running:
lsmod | grep hello

7. Unload the module
Type the following command
sudo rmmod hello
You will get a Kernel Alert message saying Goodbye, cruel world



« Last Edit: October 13, 2015, 02:22:23 PM by neowox »

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Help with compile of simple kernel module/driver
« Reply #7 on: October 13, 2015, 04:54:14 PM »
You might take it up with a kernel forum.