C Makefile - How to add header files when building (linux kernel)?

I am trying to include my own header file and its corresponding source code to my kernel module. But for some strange reason I always get the same error when making my module. Could someone explain me why and how I can fix this?

I have the following make file:

TARGET = procdriver

obj-m := procdriver.o 
procdriver-obj+= gpioLib.o

KDIR:= /home/pi/myRpi/linux
PWD := $(shell pwd)

all: gpioLib.o procdriver.c
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules 
    rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers 

gpioLib.o: gpioLib.c gpioLib.h 
    gcc -c gpioLib.c -o gpioLib.o

clean:
    make -C $(KDIR) SUBDIRS=$(PWD) clean

with the following piece "main code" which is supposed to become a .ko file for a procfs driver:

 #include <linux/module.h>
    #include <linux/proc_fs.h>
    #include <linux/seq_file.h>

    #include "gpioLib.h"  




    static int __init hello_proc_init(void) {
        int i;

        //initialize GPIO
      procFileStr = proc_create("procdriver", 0, NULL, &hello_proc_fops);
      printk(KERN_DEBUG MODULE_NAME "init procdriver!\n");
       for (i=0; i<43; i++)
       {
           gpioSetMode(i, PI_OUTPUT);  ////THIS IS THE PROBLEM
       }   

      return 0;
    }

And my header files which I am trying to include in order to keep it structured.

gpioLib.c

#include "gpioLib.h"

void gpioSetMode(unsigned gpio, unsigned mode)
{
   int reg, shift;

   reg   =  gpio/10;
   shift = (gpio%10) * 3;

   gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
}

and the corresponding gpioLib.h

#define PI_ALT3   7

#define PI_ALT4   3

#define PI_ALT5   2

void gpioSetMode(unsigned gpio, unsigned mode);

This is the error I am getting every time:

pi@raspberrypi:~/myRpi $ make
gcc -c gpioLib.c -o gpioLib.o

make -C /home/pi/myRpi/linux SUBDIRS=/home/pi/myRpi modules
make[1]: Entering directory '/home/pi/myRpi/linux'
  CC [M]  /home/pi/myRpi/procdriver.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "gpioSetMode" [/home/pi/myRpi/procdriver.ko] undefined!
  CC      /home/pi/myRpi/procdriver.mod.o
  LD [M]  /home/pi/myRpi/procdriver.ko
make[1]: Leaving directory '/home/pi/myRpi/linux'
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers

Upvotes: 1

Views: 1908

Answers (2)

The solution consisted of two parts:

1) as pointed out by @SergeyLebedev : I had to declare my function using the extern keyword.

2) the correct syntax is procdriver-objs+= gpioLib.o not procdriver-obj+= gpioLib.o

Upvotes: 0

SergeyLebedev
SergeyLebedev

Reputation: 3708

It seems that your header files and makefile are correct, but linker can't find gpioSetMode function because its name isn't available outside your gpioLib module.

In C language it is extern keyword that makes a function declared inside a module available outside that module. (In C++ hovewer extern keyword meaning is a bit different).

Add extern keyword to gpioSetMode function declaration, like so:

extern void gpioSetMode(unsigned gpio, unsigned mode);

Upvotes: 1

Related Questions