Search code examples
makefilecompilationincludeinclude-pathkbuild

How to add multiple Headers files path in a Makefile?


I am trying to compile uleds.c driver and this driver includes multiple files existing under this path :

/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/usr/src/kernel/include/linux

I want now to modify my Makefile and add this path so I can compile correctly uleds.c

This is my Makefile :

#CC=arm-poky-linux-gnueabi-gcc -march=armv7-a -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a5 --sysroot=/opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi
#CC="gcc"

obj-m += uleds.o

KERNEL_SOURCE := /opt/poky-atmel/2.5.3/sysroots/cortexa5hf-neon-poky-linux-gnueabi/lib/modules/4.14.73-linux4sam-6.0-dirty

default:
        ${CC} ${KERNEL_SOURCE} uleds.c

clean:
        ${CC} $(INC) ${KERNEL_SOURCE} clean

Any suggestions for that ? Thank you


Solution

  • This appears to be an attempt at a kbuild file,.

    You should not be manually compiling the file yourself using your default rule. Instead, you should be running the kernel's makefile, and have it compile the driver based on obj-m and friends.

    Your makefile would look like so:

    ifneq ($(KERNELRELEASE),)
    
    ccflags-y += -I some/other/dir
    obj-m += uleds.o
    
    else
    
    # default to build against running kernel if KDIR not
    # specified:
    KDIR ?= /lib/modules/`uname -r`/build
    
    default:
         $(MAKE) -C $(KDIR) M=$$PWD
    
    endif
    

    If you call make from the driver's directory, it will in turn call make from your kernel directory, which will know everything about the kernel and will be able to properly build your module.

    Notice that by default, the built-in kernel's clean target will remove all generated *.[oas] files, so no need for a special clean target. Also, by default, the kernel's makefile will include its own include directories, so you likely don't need to do anything special for that. In case you do need to include from somewhere else, you can add a -I directive to the ccflags-y as shown in the example.

    See Linux Kernel Makefiles and Building External Modules for details.