0

I'm learning Linux Kernel programming. I'm trying to build my module from multiple files. It builds and loads, but there is no messages in dmesg. What is going wrong?

solution.c:

#include <linux/init.h>
#include <linux/module.h>
#include "checker.h"

static const char* msg = "Hello from my module!";
extern void call_me(const char*);

static int __init init(void)
{
    printk(KERN_INFO "322\n");
    call_me(msg);
    printk(KERN_ALERT "After the function\n");
    return 0;
}

 static void __exit exitFromModule(void)
{
    printk(KERN_ALERT "BYE\n");
    return;
}

module_init(init);
module_exit(exitFromModule);

checker.h:

#include <linux/module.h>
 void call_me(const char* str);

checker.c:

#include "checker.h" #include <linux/kernel.h>

void call_me(const char* str)
{
    printk("<1>FROM CALL_ME!\n");
    return;
}

Makefile:

CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/misc
TARGET = solution

obj-m := $(TARGET).o
solution-objs  :=  checker.o

all:
        make -C $(KDIR) M=$(PWD)  modules

clean:
        make -C $(KDIR) M=$(PWD) clean
        @rm -f *.o .*.cmd .*.flags *.mod.c *.order;
        @rm -f .*.*.cmd *.symvers *~ *.*~ TODO.*
        @rm -fR .tmp*
        @rm -rf .tmp_versions

After that i start make and it compiles. But after insmod there is messages in dmesg.

  • Are you getting anything in dmesg? The prints from the `init` functions? Can you attach the dmesg from before and after loading the module? – Ajay Brahmakshatriya Jul 02 '21 at 19:15
  • @AjayBrahmakshatriya, no, there is nothing. – WelcomeToMyTutorial Jul 02 '21 at 19:17
  • @AjayBrahmakshatriya. sorry, i'm newbie and i'm unable to post images. There is my output. I've cleaned dmesg and it is empty after module inserting. alex@alex-Lenovo-ideapad-100-15IBD:~/l2$ sudo dmesg -c alex@alex-Lenovo-ideapad-100-15IBD:~/l2$ sudo insmod solution.ko alex@alex-Lenovo-ideapad-100-15IBD:~/l2$ sudo dmesg alex@alex-Lenovo-ideapad-100-15IBD:~/l2$ – WelcomeToMyTutorial Jul 02 '21 at 19:32
  • 1
    The line `solution-objs := checker.o` means that your module is compiled only from the ``checker.c`` file. You need to add **all** source (objects) files into `xxx-objs` list. But you cannot add object with the same name as the module itself, for that you need either to rename a module or a source file. See [that question](https://stackoverflow.com/questions/13606075/building-a-kernel-module-from-several-source-files-which-one-of-them-has-the-sam) for more info. – Tsyvarev Jul 02 '21 at 20:29
  • Does this answer your question? [Building a kernel module from several source files which one of them has the same name as the module](https://stackoverflow.com/questions/13606075/building-a-kernel-module-from-several-source-files-which-one-of-them-has-the-sam) – Tsyvarev Jul 02 '21 at 20:32
  • @Tsyvarev, well, it builds but i have an error: insmod: ERROR: could not insert module module.ko: Invalid parameters. What can be wrong? – WelcomeToMyTutorial Jul 02 '21 at 22:08
  • Probably, `dmesg` contains additional information about this error. – Tsyvarev Jul 03 '21 at 07:05
  • @Tsyvarev, yes. you're right. Thank you. – WelcomeToMyTutorial Jul 03 '21 at 10:00

0 Answers0