aldeb
aldeb

Reputation: 6838

`module_init` function refuse to execute as a result of splitting the module into multiple source files

I'm currently working on a kernel module. Here is a minimalistic module that reproduces my problem.

main_module.c:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>


#include "other_functions.h"

MODULE_LICENSE("GPL");
int major = 0;

struct file_operations fops = {
    .owner = THIS_MODULE,
};

int __init main_module_init(void) {
    printk(KERN_INFO "<main_module> Hello World!\n");
    major = register_chrdev(0, "main_module", &fops);
    test();
    return 0;
}

void __exit main_module_exit(void) {
    unregister_chrdev(major, "main_module");
    printk(KERN_INFO "<main_module> goodbye\n");
}

module_init(main_module_init);
module_exit(main_module_exit);

other_functions.c:

#include "other_functions.h"

int test(void) {return 0;}

other_functions.h:

#ifndef OTHER_FUNCTIONS
#define OTHER_FUNCTIONS

#include <linux/module.h>

MODULE_LICENSE("GPL"); 

int test(void);

#endif

Makefile:

obj-m += main_module.o

main_module-objs := other_functions.o

default:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

After I compiled the module and loaded it, dmesg | grep main_module outputs nothing whereas I was supposed to get <main_module> Hello World! as output.

However if I remove the line 4 in main_module.c: #include "other_functions.h, and the line 3 in the Makefile: main_module_objs := .... The dmesg | grep main_module command will give me the requested output after recompiling the module and reloading it.

Could somebody please explain me how to fix that? I really don't manage to understand this behavior. Thanks a lot!

PS: I'm working on Ubuntu 12.04 and my kernel version is 3.2.0-37-generic-pae

Upvotes: 0

Views: 276

Answers (1)

aldeb
aldeb

Reputation: 6838

I finally figured out what was going wrong. The problem was in the Makefile. The object file main_module.o was not linked. So I edited the makefile and that solved the problem:

Makefile:

obj-m += target.o # Here I changed the target's name so it's not interfering with any other dependency

target-objs := main_module.o other_functions.o # I added main_module to the dependencies

default:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

Upvotes: 1

Related Questions