0

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

S2G
  • 121
  • 7
  • 2
    Are you _required_ to use cmake? Don't use it. – KamilCuk Dec 28 '21 at 10:47
  • actually I am not , I just found Cmake on the internet some people use it to make makefiles and to include paths and ... – S2G Dec 28 '21 at 10:50
  • 3
    You can't have main or printf in a kernel module. Get the book Linux Device Drivers and start from there. – stark Dec 28 '21 at 10:52
  • @stark so what u are basically telling me is that i only can make libraries base on linux-kernel and then include these libraries inside of an app ? – S2G Dec 28 '21 at 10:54
  • If you are writing a library or an app it runs in user space, not kernel. – stark Dec 28 '21 at 11:56
  • @stark i am trying to develop in program (GUI either with QT/Flutter/JAVA - not decided yet) that can communicate through I2C/serial , i don't want to use already available raspberry drivers , since i need to implement it on another chip (Renesas) soon, which has HW CAN communication (my final goal) i have like 6 month and started learning linux for a month now , there is a chance that i am going the wrong way completely – S2G Dec 28 '21 at 12:05
  • Unless you are designing new hardware, you can build a kernel that supports it without writing a line of kernel code. – stark Dec 28 '21 at 12:13
  • `target_include_directories (driver PUBLIC usr/src/linux-headers-5.4.19-91/include)` uses a relative path, i.e. assuming the `CMakeLists.txt` file recides in directory `/foo/bar` the include path you're adding for the `driver` target is `/foo/bar/usr/src/linux-headers-5.4.19-91/include` instead of `/usr/src/linux-headers-5.4.19-91/include`; you need to use absolute paths in cmake here... – fabian Dec 28 '21 at 13:15

1 Answers1

3

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111