0

I have several files: so called library and kernel module, which uses functions from this library. All files are in the same directory.

lib.c:

#include <linux/module.h>    
#include "lib.h"   

volatile int check = 0;

int lib_in(void)
{
     check = 5;
     printk(KERN_ERR "In -> check:%d\n", check);
     return 0;
}
void lib_out(void) { printk(KERN_ERR "Out\n"); }
MODULE_LICENSE("GPL");

lib.h:

int lib_in(void);
void lib_out(void);

some_mod.c:

#include <linux/init.h>    
#include <linux/module.h>    
#include <asm/string.h>    
#include "lib.h"   

static int __init mod_init(void)
{
    printk(KERN_ERR "Loaded\n");
    return lib_in();
}

static void ______wtf; mod_exit(void) // syntax error
{
    printk(KERN_ERR "Unloaded\n");
    lib_out();
}

Plain text - here also should be syntax error

MODULE_LICENSE("GPL");
module_init(mod_init);
module_exit(mod_exit);

While build it somehow doesn't compile some_mod object file (and even check its code), but it emits some_mod.ko. By the way it compiles lib.c. So the module could be correctly inserted but there is no output from printk.
P.S. the errors in some_mod.c were added to underline that this code is not even compiled. That is the problem.

Makefile:

KDIR = /lib/modules/$(shell uname -r)/build
PWD = $(shell pwd)

TARGET = some_mod
LIB = lib 

obj-m := $(TARGET).o
#$(TARGET)-y := $(LIB).o
$(TARGET)-objs := $(LIB).o

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

Can someone tell me what am I doing wrong? Thanks in advance.

NK-cell
  • 1,145
  • 6
  • 19
  • The problem is that you want to build a module from several files one of which has the same name as the module itself. See there how to handle that: [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 Jun 04 '20 at 17:56

1 Answers1

0

The wtf line reads like a mix of a symbol definition error static void ______wtf; and mod_exit() missing a type. Try replacing the line with:

static void __exit mod_exit(void)

Delete the line with Plain and rebuild.

After no errors, a successful compile and link, and successful insmod, output from printk() should be visible on console, the system messages file, or both.

BTW, if you see any build errors about unresolved symbols, review your Makefile for .o files.

Milag
  • 1,793
  • 2
  • 9
  • 8
  • I have no errors. That's the problem. I mentioned that it successfully builds and inserts. Without compiling the code in *some_mod.c*. – NK-cell Jun 04 '20 at 17:51
  • *"should be visible"* - you can try it to make sure. If code does not compile - errors in it have no matter. – NK-cell Jun 04 '20 at 17:55
  • There's something strange here. Try renaming, eg `mv some_mod.c sm.c`; add `sm.o` to the line `$(TARGET)-objs` . What are your results? – Milag Jun 04 '20 at 18:44
  • Answers in question mentioned by Tsyvarev helped. The name of target must be different from the source file names. – NK-cell Jun 05 '20 at 09:23