Find the answer to your Linux question:
Results 1 to 3 of 3
Hi people! I'm learning how to program kernel drivers, and I'm making a hello world module for that. But I can't find the cause of some errors... My module as ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    1

    Question error: function undeclared. but it is declared!

    Hi people!
    I'm learning how to program kernel drivers, and I'm making a hello world module for that. But I can't find the cause of some errors...
    My module as three files:

    test_regulator.c:
    Code:
    /*test_regulator.c
      2 */
      3 #include <linux/test_regulator.h>
      4 #include <linux/module.h>
      5 #include <linux/kernel.h>
      6 #include <linux/regulator/driver.h>
      7 #include <linux/platform_device.h>
      8
      9
     10 /*
     11    * do the init function
     12    */
     13    int __init test_init(void)
     14   {
     15          //phello=hello_world;
     16          //pbye=bye_bye_world;
     17
     18          struct test_regulator ret;
     19          platform_driver_register(&test_regulator_driver);
     20           printk("test regulator charged\n");
     21          /*call the test function for the void pointers*/
     22          ret.test_inteiro = 2;
     23          ret.test_ponteiro = &ret.test_inteiro; //test_ponteiro vai sempre apontar para test_inteiro
     24           //ret.test_inteiro = (*phello)(ret.test_ponteiro);
     25           ret.test_inteiro = hello_world(ret.test_ponteiro);
     26         printk("retorno da funcao hello_world %d \n",ret.test_inteiro);
     27          //ret.test_inteiro = (*pbye)(ret.test_ponteiro);
     28          ret.test_inteiro = bye_bye_world(ret.test_ponteiro);
     29          printk("retorno da funcao bye bye world %d \n",ret.test_inteiro);
     30           return 0;
     31   }
     32
     33   /*
     34    * do the release function
     35    */
     36   void __exit test_exit(void)
     37   {
     38           printk("test regulator released\n");
     39         platform_driver_unregister(&test_regulator_driver);
     40   }
     41
     42 static struct platform_driver test_regulator_driver = {
     43    .probe        = hello_world,
     44    .remove        = bye_bye_world,
     45    .driver        = {
     46        .name        = "test_regulator",
     47    },
     48 };
     49
     50
     51   /*
     52    * register the init and exit functions
     53    */
     54   module_init(test_init);
     55   module_exit(test_exit);
     56  MODULE_LICENSE("GPL");
    functions_regulator.c:

    Code:
    *functions_regulator.c
      2 */
      3 #include <linux/test_regulator.h>
      4 #include <linux/module.h>
      5  #include <linux/kernel.h>
      6  #include <linux/regulator/driver.h>
      7  #include <linux/platform_device.h>
      8
      9
     10 int hello_world(int *ret)
     11 {
     12         int x = 5;
     13  /*
     14          printk("O valor de x eh: %d \n",x);
     15          void *y = &x; //declaraçao de um ponteiro y que aponta para o endereço de x (recebe como valor o endereço de x)
     16          int *z;       //declara z como um ponteiro de inteiros
     17          z = y;        //z recebe como valor, o endereço de x
     18          int w = z;
     19          printk("O endereço de x, que foi passado para z é: %d \n",w);*/
     20          printk("variavel recebida: %d\n",*ret);
     21          x = *ret;
     22          x = x*2;
     23          *ret = x;
     24          printk("hello world variavel recebida: %d\n",x);
     25          return x;
     26 }
     27
     28 /*
     29  bye bye world function
     30  */
     31 int bye_bye_world(int *ret)
     32  //float bye_bye_world(int ret)
     33  {
     34          ter.test_inteiro = 3;
     35          int x;
     36         x = *ret;
     37          printk("bye bye world variavel recebida: %d\n",x);
     38          ter.test_inteiro= x*ter.test_inteiro;
     39          printk("hello world ret * ter.test_int: %d\n",ter.test_inteiro);
     40          return ter.test_inteiro;
     41  }
    and test_regulator.h:
    Code:
    /*test_regulator.h
      2 */
      3 #include <linux/module.h>
      4 #include <linux/kernel.h>
      5 #include <linux/regulator/driver.h>
      6 #include <linux/platform_device.h>
      7
      8 struct test_regulator {
      9         int *test_ponteiro, test_inteiro;
     10 }ter;
     11
     12 static int hello_world (int *ret);
     13 static int bye_bye_world (int *ret);
     14
     15 int (*phello) (int);
     16 int (*pbye) (int);
    the other files included are files from other drivers, they are in /usr/src/linux/drivers/regulator and in /usr/src/linux/include/linux and they have some structures that I am using.

    I changed the Makefile and Kconfig files in /usr/src/linux to include my module and also included it with make menuconfig.

    so, when I do make modules, I have the following:

    CHK include/linux/version.h
    CHK include/linux/utsrelease.h
    SYMLINK include/asm -> arch/x86/include/asm
    CALL scripts/checksyscalls.sh
    CC [M] drivers/regulator/test_regulator.o
    drivers/regulator/test_regulator.c: In function ‘test_init’:
    drivers/regulator/test_regulator.c:19: error: ‘test_regulator_driver’ undeclared (first use in this function)
    drivers/regulator/test_regulator.c:19: error: (Each undeclared identifier is reported only once
    drivers/regulator/test_regulator.c:19: error: for each function it appears in.)
    drivers/regulator/test_regulator.c: In function ‘test_exit’:
    drivers/regulator/test_regulator.c:39: error: ‘test_regulator_driver’ undeclared (first use in this function)
    drivers/regulator/test_regulator.c: At top level:
    drivers/regulator/test_regulator.c:43: warning: initialization from incompatible pointer type
    drivers/regulator/test_regulator.c:44: warning: initialization from incompatible pointer type
    make[2]: *** [drivers/regulator/test_regulator.o] Erreur 1
    make[1]: *** [drivers/regulator] Erreur 2
    make: *** [drivers] Erreur 2

    I don't understand, because I had declared the test_regulator_driver.

    Has anyone an idea to help me?

    thanks

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    I think you need to declare variables before you use them, not after.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    hazel is completely correct. The variable must be defined before you use it. For functions, because sometimes you might have two functions that refer to each other, you can define the interface to a function in one place, and the body somewhere else, but variables must all be defined before use.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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