13

I developed two simple modules to the kernel. Now i want to define a function in one module and after that use it in the other.

How i can do that?

Just define the function and caller in the other module without problems?

Braiam
  • 1
  • 11
  • 47
  • 78
Ricardo
  • 612
  • 2
  • 9
  • 16
  • Possible duplicate of [How to call exported kernel module functions from another module?](http://stackoverflow.com/questions/12311867/how-to-call-exported-kernel-module-functions-from-another-module) – Ciro Santilli OurBigBook.com May 20 '17 at 12:27

1 Answers1

36

Define it in module1.c:

#include <linux/module.h>

int fun(void);
EXPORT_SYMBOL(fun);

int fun(void)
{
    /* ... */
}

And use it in module2.c:

extern int fun(void);
Jeff Allen
  • 1,391
  • 13
  • 19
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • I still have a problem. In the directory /var/log/ i open the file messages to see the printk done buy my function. Appears a message like this :" module license "unspecified" taints kernel","Disabling lock debugging due to kernel taint". I just call the function of one module to print a message in the other module. – Ricardo Mar 22 '12 at 11:01
  • 2
    @Ricardo: That's not related to exporting functions, but simply to the fact, that you didn't define the module's license. Use the `MODULE_LICENSE` macro to do that. – Jan Hudec Mar 22 '12 at 11:10
  • Is working. Thank you. For modules inside the kernel image, is the same process? – Ricardo Mar 22 '12 at 13:40
  • What happens if you are adding these modules to a Linux kernel and you set both of them to compile as modules? When trying `make`, the kernel will not compile, because it cannot resolve the reference to foo() in module2.c, right? How could it be solved? @JanHudec – marcocamejo Jul 13 '12 at 02:16
  • @marcocamejo: You need to specify that module2 depends on module1, but I don't know the exact details. – Jan Hudec Jul 23 '12 at 09:30
  • I can't seem to insmod module2.ko. Am I missing something? – Moirisa Dikaiosýni Feb 13 '14 at 05:22
  • @cnicutar: I think the declration in the `module2.c` should be `extern int fun(void);` instead of `foo`. – brokenfoot Mar 22 '14 at 02:48