How to build a module with source codes in separate directories.

If you want to use separate directories for source files and target modules, you can use below scheme.

mod_main.c

#include 
#include 

MODULE_LICENSE("GPL");

static int __init mod_entry(void)
{
        return 0;
}

static void __exit mod_exit(void)
{
        return;
}

module_init(mod_entry);
module_exit(mod_exit);

sub_mod.c

#include 
#include 

int sub_func(int i)
{
        printk("Hello %d", i);
        return 0;
}

EXPORT_SYMBOL(sub_func);

http://pagead2.googlesyndication.com/pagead/show_ads.js

Makefile

obj-m += target/simple.o

target/simple-objs := src/main_mod.o src/sub_mod.o

export KROOT=/lib/modules/`uname -r`/build

allofit: modules
modules:
        @$(MAKE) -C $(KROOT) M=$(PWD) modules
modules_install:
        @$(MAKE) -C $(KROOT) M=$(PWD) modules_install
clean:
        rm -rf src/*.o target/*.ko target/*.cmd target/*.mod.c .tmp_versions .*.d *.tmp Module.symvers

The result directory would be like below.

[root@localhost module]# tree -a
.
|-- Makefile
|-- modules.order
|-- Module.symvers
|-- src
|   |-- main_mod.c
|   |-- main_mod.o
|   |-- .main_mod.o.cmd
|   |-- sub_mod.c
|   |-- sub_mod.o
|   `-- .sub_mod.o.cmd
|-- target
|   |-- simple.ko
|   |-- .simple.ko.cmd
|   |-- simple.mod.c
|   |-- simple.mod.o
|   |-- .simple.mod.o.cmd
|   |-- simple.o
|   `-- .simple.o.cmd
`-- .tmp_versions
    `-- simple.mod

3 directories, 17 files

Leave a Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.