# How to setup inter-module function calls



## chodong (May 13, 2010)

I have developed two modules (A & B) where B depends on A. Module B needs to call function func1 provided by module A.

I successfully kldload module A first, but I cannot load module B due to a linker error:


```
kernel: link_elf: symbol func1 undefined
```

How do I mark func1 for export in module A so the linker can resolve it for module B?

In Linux I would simply add EXPORT_SYMBOL(func1) in module A.

Thank you,


----------



## trasz@ (May 13, 2010)

How is the "func1" defined?  Would be great if you could paste appropriate part of your code.


----------



## eyebone (May 13, 2010)

i would be interested as well, also if you are loading Module B automaticly via the loading of Module A?


----------



## chodong (May 13, 2010)

I have the following setup:

1. Module A:
File A.h that defines func1 and func2 as follows:


```
int func1(int flag);
extern int func2(int flag); /* try extern as well. */
```
File A.c declares func1 and func2 as follows:


```
int func1(int flag) {return flag;}
int func2(int flag) {return flag;}
```

2. Module B:
File B.c includes A.h and tries to call func1 and func2.


I first kldload A.ko and upon success I try to load B.ko but this fails because func1 and func2 are undefined.


----------



## chodong (May 14, 2010)

The problem is solved.

I added to module B MODULE_DEPEND(A) and func1 and func2 are resolved and I can load module B after loading module A.


----------



## williamy (Apr 25, 2014)

I met this issue, and still looking for an example, 
Can someone please give an simple sample , thanks


----------



## iulian (May 26, 2021)

What chodong says is right in order to achive EXPORT_SYMBOL effect on FreeBSD you need to use MODULE_DEPEND(9), but you also need to keep in mind that:
-the prototype of your functions need to be declared nonstatic otherwise compiler will try to link against them right after compile time.


----------

