2

Say I have the following structure:

.\
 |_ foo.c
 |_ inc\
     |_ foo_helper.c
     |_ foo_helper.h

I want to obtain foo.ko. foo.c should #include <foo_helper.h>. Also, a debug message with printk is placed in the foo.c init function, just to see if the function was loaded. My Kbuild file is:

obj-m := foo.o
foo-y := inc/foo_helper.o

ccflags-y := -I$(src)/inc/

Compiles fine. insmod returns 0; lsmod lists the module as loaded, but the debug message is not printed in kern.log.

A minimal example:

  • foo.c:
#undef __KERNEL__
#define __KERNEL__
#undef MODULE
#define MODULE

#include <linux/module.h>   // included for all kernel modules
#include <linux/kernel.h>   // included for KERN_INFO

#include <foo_helper.h>

static int __init foo_init(void)
{
    printk(KERN_INFO "Foo inserted successfully.\n");

    foo_help_me();

    return 0;
}

static void __exit foo_exit(void){}
module_init(foo_init);
module_exit(foo_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Foo");
MODULE_DESCRIPTION("Bar");
  • inc/foo_helper.h:
#ifndef __FOO_CUSTOM_HELPER
#define __FOO_CUSTOM_HELPER

#include <linux/kernel.h>

void foo_help_me(void);

#endif  /* __FOO_CUSTOM_HELPER */
  • inc/foo_helper.c:
#include "foo_helper.h"

void foo_help_me(void)
{
  printk(KERN_INFO "We're inside the helper, seems to be working.\n");
}
  • Kbuild:
obj-m := foo.o
foo-y := inc/foo_helper.o
ccflags-y := -I$(src)/inc
  • Makefile:
ifneq ($(KERNELRELEASE),)
include Kbuild
else
KDIR := /lib/modules/`uname -r`/build
default:
  $(MAKE) -C $(KDIR) M=$$PWD modules
endif
adizere
  • 224
  • 1
  • 10

1 Answers1

1

I found the solution in the meantime at: tldp.org. The only modifications needed are in the Kbuild file, as follows:

obj-m := fool.o
fool-objs := foo.o inc/foo_helper.o
ccflags-y := -I$(src)/inc/

So I'll need to link the two intermediary objects (foo and foo_helper) in order to obtain a third, final one, in this case I called it fool.

adizere
  • 224
  • 1
  • 10