2010-12-22 08:16:50     Building Modules with multiple source files

Document created by Aaronwu Employee on Aug 25, 2013
Version 1Show Document
  • View in full screen mode

2010-12-22 08:16:50     Building Modules with multiple source files

Prasanth Rajagopal (INDIA)

Message: 96950   

 

I am trying to build a simple test Module that has multiple files, but could not make it work even after referring a number of Linux discussions.

 

Usually I would have this for single file::-

 

obj-m := test_drv.o

 

I want to call a function extern_func() in extern_file.c from test_drv.c,

 

I trie::-

 

obj-m := test_drv.o

 

test_drv-objs := extern_file.o

 

Build module from eclipse, and I get

 

make: Entering directory `/prasanth-work/Linux/Kernel/Linphone-test/uClinux-dist/linux-2.6.x'

  CC [M]  /root/workspace/galileo/fprint_bfin_driver/test.o

  LD [M]  /root/workspace/galileo/fprint_bfin_driver/fprint_bfin_driver.o

  Building modules, stage 2.

  MODPOST 1 modules

  LD [M]  /root/workspace/galileo/fprint_bfin_driver/fprint_bfin_driver.ko

make: Leaving directory `/prasanth-work/Linux/Kernel/Linphone-test/uClinux-dist/linux-2.6.x'

 

I have inserted printk in test_drv.c & test_drv.c and neither prints, but insmod doesnt show an error. If I build test_drv.c without extern_file.c, I can always get the prinrtk to work.

 

I tried suggestions from a couple of threads, but in vain.

 

  www.linuxquestions.org/questions/programming-9/kernel-module-with-multiple-source-files-compilation-problem-441708/

 

  www.linuxquestions.org/questions/programming-9/linking-multiple-files-kernel-module-programming-701735/

 

  linux.die.net/lkmpg/x351.html

 

Any thoughts?

QuoteReplyEditDelete

 

 

2010-12-22 09:30:08     Re: Building Modules with multiple source files

Mike Frysinger (UNITED STATES)

Message: 96957   

 

please review the documentation:

  docs.blackfin.uclinux.org/doku.php?id=linux-kernel:modules:example

QuoteReplyEditDelete

 

 

2010-12-22 09:46:02     Re: Building Modules with multiple source files

Prasanth Rajagopal (INDIA)

Message: 96959   

 

Thanks, but I thought that's what I have been trying, as I mentioned:

 

obj-m := test_drv.o

 

test_drv-objs := extern_file.o

 

Did I really miss something?

QuoteReplyEditDelete

 

 

2010-12-22 09:52:26     Re: Building Modules with multiple source files

Mike Frysinger (UNITED STATES)

Message: 96960   

 

i really cant tell.  you havent posted any actual code, just vague examples that dont reflect what you're actually doing.

QuoteReplyEditDelete

 

 

2010-12-23 05:10:04     Re: Building Modules with multiple source files

Prasanth Rajagopal (INDIA)

Message: 96968   

 

Here is my module code:

 

 

#include <linux/init.h>

#include <linux/module.h>

 

extern void func1(void);

 

MODULE_LICENSE("Dual BSD/GPL");

 

static int fprint_init(void)

{

        printk("\nfprint driver loaded\n");

 

        func1();

 

        printk("\nfprint driver loaded\n");

 

        return 0;

}

 

static void fprint_exit(void)

{

        printk("\nfprint driver unloaded\n");

}

 

 

module_init(fprint_init);

module_exit(fprint_exit);

 

 

And the dependent file::-

 

#include <linux/init.h>

#include <linux/module.h>

 

void func1(void);

 

void func1()

{

    printk("\nhello\n");

 

}

 

I have played around with the func declarations and but could not resolve it (with extern, without extern, without declarations - just to see how the error changes). Basically I just want to call a function in another file.

QuoteReplyEditDelete

 

 

2010-12-24 10:47:37     Re: Building Modules with multiple source files

Mike Frysinger (UNITED STATES)

Message: 96984   

 

works fine for me

 

$ cat Makefile

obj-m := test.o

test-objs := file1.o file2.o

 

$ make -C /usr/local/src/blackfin/git/linux-kernel/ SUBDIRS=$PWD modules

make: Entering directory `/usr/local/src/blackfin/git/linux-kernel'

  CC [M]  /usr/local/src/u-boot/tmpu/foo/file1.o

  CC [M]  /usr/local/src/u-boot/tmpu/foo/file2.o

  LD [M]  /usr/local/src/u-boot/tmpu/foo/test.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /usr/local/src/u-boot/tmpu/foo/test.mod.o

  LD [M]  /usr/local/src/u-boot/tmpu/foo/test.ko

make: Leaving directory `/usr/local/src/blackfin/git/linux-kernel'

 

$ rcp test.ko root@bfin:/

 

root:/> insmod test.ko

 

fprint driver loaded

 

hello

 

fprint driver loaded

root:/> rmmod test.ko

 

fprint driver unloaded

QuoteReplyEditDelete

 

 

2010-12-24 11:26:54     Re: Building Modules with multiple source files

Prasanth Rajagopal (INDIA)

Message: 96987   

 

Strange, I did get a couple of hits like the links I mentioned, but no luck . Any thoughts? Has it to do with Host?

 

cat /etc/SuSE-release

openSUSE 11.0 (i586)

VERSION = 11.0

 

QuoteReplyEditDelete

 

 

2010-12-27 02:32:43     Re: Building Modules with multiple source files

Sonic Zhang (CHINA)

Message: 96999   

 

Strange, I did get a couple of hits like the links I mentioned, but no luck . Any thoughts? Has it to do with Host?

 

cat /etc/SuSE-release

openSUSE 11.0 (i586)

VERSION = 11.0

 

 

 

---

 

It looks you forget to put test_drv.o after extern_file.o.

 

obj-m := test.o

 

test-objs := extern_file.o test_drv.o

QuoteReplyEditDelete

 

 

2010-12-28 05:06:47     Re: Building Modules with multiple source files

Prasanth Rajagopal (INDIA)

Message: 97030   

 

Thanks, that made it work!

 

So my mistake was::

 

obj-m := test_drv.o

 

test_drv-objs := extern_file.o

 

The correct way was::

 

obj-m := test.o

 

test-objs := extern_file.o test_drv.o

 

My source files are only test_drv.c and extern_file.c, where test_drv is the actual module file. Why should the module object name be built in a separate name....!

 

In this example, the module name appears to be taken from the modules file name and then dependencies are added.

 

  docs.blackfin.uclinux.org/doku.php?id=linux-kernel:modules:example

 

And in this example, it appears like the module as such is divided in to several files.

 

  docs.blackfin.uclinux.org/doku.php?id=linux-kernel:modules:building

 

After some searching, I found the actual reason as explained from modules.txt (linux-2.6.x/Documentation/kbuild).

 

--- 3.3 Loadable module goals - obj-m

 

    $(obj-m) specify object files which are built as loadable

    kernel modules.

 

    A module may be built from one source file or several source

    files. In the case of one source file, the kbuild makefile

    simply adds the file to $(obj-m).

 

    Example:

        #drivers/isdn/i4l/Makefile

        obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o

 

    Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to 'm'

 

    If a kernel module is built from several source files, you specify

    that you want to build a module in the same way as above.

 

    Kbuild needs to know which the parts that you want to build your

    module from, so you have to tell it by setting an

    $(<module_name>-objs) variable.

 

    Example:

        #drivers/isdn/i4l/Makefile

        obj-$(CONFIG_ISDN) += isdn.o

        isdn-objs := isdn_net_lib.o isdn_v110.o isdn_common.o

 

    In this example, the module name will be isdn.o. Kbuild will

    compile the objects listed in $(isdn-objs) and then run

    "$(LD) -r" on the list of these files to generate isdn.o.

Attachments

    Outcomes