Home > Net >  linux header include fails in C program
linux header include fails in C program

Time:12-29

i am an embedded developer and quite new to the Linux world. my task is to write a fairly simple driver which i am to understand the need for linux-kernel modules .

i do have a simple c file which iam trying to compile but with no success yet

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


void main (void)
{
    printf("Hi There ! \n");
}

i get the error init.h and ioport.h files are not found

the compatible version of linux-header is now installed on my pc using apt pm.

these files are not in usr/include/linux but they are stored in usr/src/linux-headers-5.4.19-91/include .

so i tried to add the path of usr/src/linux-headers-5.4.19-91/include using export PATH - didnt work

tried to use the makefile inside usr/src/linux-headers-5.4.19-91/ hoping that it would be installed next but i am getting no rule to make target arch/x86/tools/relocs_32.c

and now i am trying to include this path to my CMake file (rn i am noob in cmake) using this code :

target_include_directories (driver PUBLIC usr/src/linux-headers-5.4.19-91/include)
target_include_directories (driver PUBLIC usr/src/linux-headers-5.4.19-91/include/config)
target_include_directories (driver PUBLIC usr/src/linux-headers-5.4.19-91)

again it dosent work . is there any way for me to include the files not using the Cmake or if i have to how can i include them ? do they need to be make install inside the library it self ?

EDIT: thanks to @KamilCuk for the answer which pushed me on the right track , I also found this link which has an amazing tutorial for people who just want to start System Programming .

https://sysplay.github.io/books/LinuxDrivers/book/Content/Part01.html

CodePudding user response:

his path to my CMake file

Linux Kernel Development is done with Make with a small Makefile script.

Do not use CMake. While it may be possible to integrate CMake with kernel development, I believe it would require substantial work and if you are learning, definitely out of scope.

how can i include them ?

They are just included by themselves by Kernel Makefile scripts. You can read documentation https://www.kernel.org/doc/html/latest/kbuild/modules.html , but really all that is needed to start is a short Makefile file with only 3 lines.

Create a file called Makefile with just 5 lines as presented here Makefile for Linux kernel module? and then type make. The chardev.o matches the file chardev.c with the source - change it if your file has different name.

do they need to be make install inside the library it self ?

No.

in usr/include/linux but they are stored in usr/src/linux-headers-5.4.19-91/include .

There is user-space and kernel-space. To compiler a user-space program, /usr/include/linux is used. To compile a kernel module linux-headers are used. They are unrelated and shouldn't be mixed.

Linux Kernel is not like writing normal programs in user-space. There is no main nor printf in the kernel.

  • Related