Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I am writing a Linux Device driver for 2.6 kernel (for the first time). There is plenty of code so I decided to split it into 2 files. I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    10

    Global variables in multi-file device driver


    Hi,

    I am writing a Linux Device driver for 2.6 kernel (for the first time). There is plenty of code so I decided to split it into 2 files. I have 2 global variables that I want both files to use. I could use function parameters but that would be way too cumbersome unfortunately. I can't get this to work though.
    ----------
    Code:
    first.c
    #include <linux/module.h>
    ....
    ....
    char *mem_ptr;
    
    static int __init my_init()  {
    ....
    }
    ----------
    Code:
    second.c
    #include <linux/module.h>
    ....
    ....
    extern char *mem_ptr;
    
    function compute()  {
       mem_ptr = NULL;
    ....
    }
    I get a build (link) time warning as follows:

    Code:
      Building modules, stage 2.
      MODPOST
    *** Warning: "mem_ptr" [/home/guraaf/first.ko] undefined!
    Any ideas why doesn't the extern pick up the global variable from the other file? Do I have my makefile incorrectly defined. It is:

    Code:
    obj-m   := first.o 
    first-objs := first.o second.o
    
    module:
            $(MAKE) -C $(KERNELDIR) ARCH=ppc M=$(PWD) modules

  2. #2
    Just Joined!
    Join Date
    Jul 2009
    Posts
    10
    in second.c file

    char variable;
    EXPORT_SYMBOL(variable);

    and use it in first.c file..the above will export the variable to global symbol table so that u can use it anywhere in any module.. remove the extern and try once..

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Posts
    10
    Hi,
    Thanks for replying. The problem was that the .tmp_versions got into a bad state. As soon as I deleted that direcory everything started working.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •