I'm trying to build a loadable kernel module using multiple source files. According to section 3.3 of https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt I've got to use obj-m for main object file and modulename-y for the rest of it. Here's my mwe:
helpers.h
#ifndef __HELPERS_H__
#define __HELPERS_H__
void helper_print_init(void);
void helper_print_exit(void);
#endif // __HELPERS_H__
helpers.c
#include "helpers.h"
#include <linux/kernel.h>
void helper_print_init(void) {
printk("multi_file_ko_init_helper\n");
}
void helper_print_exit(void) {
printk("multi_file_ko_exit_helper\n");
}
multiFileKo.c
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
//#include "helpers.h"
static int __init multi_file_ko_init(void) {
printk("multi_file_ko_init\n");
// helper_print_init();
return 0;
}
static void __exit multi_file_ko_exit(void) {
printk("multi_file_ko_exit\n");
// helper_print_exit();
}
module_init(multi_file_ko_init);
module_exit(multi_file_ko_exit);
MODULE_LICENSE("MIT");
MODULE_AUTHOR("AUTHOR");
MODULE_DESCRIPTION("gpio");
MODULE_VERSION("0.0");
NOTE THAT multiFileKo.c does not even actually uses helpers for now. I tried to actually call those functions but for simplicity just commented things out from mwe.
Now if I compile it with kbuild like follows, using only main file, I get dmesg output as expected:
obj-m := multiFileKo.o
But when I try to compile it linked with helpers, even without actually using them as follows, dmesg remains silent even though insmod/rmmod seem to be working:
obj-m := multiFileKo.o
multiFileKo-y := helpers.o
Obviously if I uncomment everything in multiFileKo.c it does not work either. So the fact of linking additional object file seems to be breaking my module regardless of what that additional object file does.
Approach with multiFileKo-objs does not work for me either. I saw this earlier, but sure where this takes it's origin, since makefiles manual uses it only in context of host programs.