If I have a function that is in one Linux Kernel, but not others, how would I go about using the same source code to compile for different kernels?
For example, if functionA
is an old function which both kernels support, but functionB
is only supported in newer kernels and I want to use functionB
when I can for the systems that support it, how do I have only one piece of code?
Is there a way to dynamically try to load the function by name functionB
and then if it fails, go ahead and dynamically load functionA
?
You can only compile a module for a specific kernel version, other kernels will refuse to load it. You will therefore need to compile it once for every kernel version that you need to support, thus you can simply use compile-time macros such as LINUX_VERSION_CODE
and KERNEL_VERSION()
:
static void do_something(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,0,0)
functionB();
#else
functionA();
#endif
}
Furthermore, make sure to check whether functionB()
is behind some CONFIG_
option: in such case you also want to wrap everything in a #ifdef CONFIG_THAT_ENABLES_FUNCTION_B
.