How to pass parameters from makefile to Linux kernel module source code

Rasmus Friis Kjeldsen :

I have the source code:

#include <linux/module.h>   
#include <linux/kernel.h>   

int init_module(void)
{
    printk(KERN_INFO "Hello world %i\n", BUILD_NUMBER);
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}

and a makefile:

obj-m += hello-1.o

BUILD_NUMBER := 42

# How to pass BUILD_NUMBER to hello-1.c ???

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Now how do I pass the BUILD_NUMBER parameter from the makefile to the source code?

Didier Trosset :

As the Linux build system uses kernel provided Makefiles that can reasonably not be changed. I'd suggest to place your version number directly into the source code instead of in the Makefile.

There's a possibility thought. You can define a CPPFLAGS environment variable. It should be passed by the kernel Makefile to the C compiler command line. If you define this CPPFLAGS variable as -DVERSION=42, maybe you can use this VERSION macro inside your source file.

all:
    CPPFLAGS="-DVERSION=42" make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

Note that CPPFLAGS stands for "C PreProcessor FLAGS". It is unrelated to C++.

After testing it. This does not work. There's a solution however. The kernel Makefile allows (and uses) the definition of the KCPPFLAGS environment variable that will be added to the kernel Makefile defined own CPPFLAGS.

You have to use:

all:
    KCPPFLAGS="-DVERSION=42" make -C /lib/modules/$(shell uname -r)/build M=$(PWD) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Makefile for Linux kernel module?

How to code a Linux kernel module?

Linux kernel module programming: makefile

Makefile for linux kernel module issue

How to patch Linux kernel source code in yotco

How to create a device node from the init_module code of a Linux kernel module?

How to get hostname from linux kernel module?

How to write a linux kernel module to modify source MAC address of a packet?

Compiling a kernel module using makefile in linux

How to make a minimal bootable linux (only with terminal) from kernel source code?

How to pass a value to a builtin Linux kernel module at boot time?

reading linux kernel source code

Changing kernel variables/parameters from a kernel module

How to pass parameters from command line to a java program through makefile

how to locate source code path from module

Calling Linux kernel methods from a kernel module

How can I clone a module from linux kernel?

How to read a string from a sysfs attribute file a Linux kernel module?

How to get the physical address from the logical one in a Linux kernel module?

How does linux kernel Makefile understands .config?

Makefile for a basic kernel module

How to put a check in the code to ensure the inter kernel module dependency - Linux Kernel?

How to pass in parameters to a dagger module from a activity or fragment at runtime

Linux kernel module Makefile can't include relative paths

Please explain linux kernel source code directories

How to install kernel source from upstream kernel

How to download code of latest Linux kernel From development branch of Linux kernel maintainers?

How to reference a data source from a module to another module and pass it as a variable to root module?

How to load Linux kernel modules from C code?