Comments on: C++ in Linux kernel https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/ Programming, electronics and diy projects Mon, 05 Jun 2023 16:48:52 +0000 hourly 1 https://wordpress.org/?v=6.5.3 By: Johannes https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1713 Mon, 05 Jun 2023 16:48:52 +0000 http://olegkutkov.me/?p=1017#comment-1713 Hi Oleg,

it’s been quite a while since I did my last tests with this, but now I decided to give it again a chance and now I’m facing some problems with the include directories and include files used in my cpp files. For example I wanted to use a spinlock_t in my C++ class but I always get “error: ‘spinlock_t’ does not name a type”. But the spinlock works inside the same project when used in a c file. Do you have any idea how I could adjust the include directories for those kernel header files to also work for C++ files?

Best regards,
Johannes

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1400 Wed, 07 Dec 2022 23:17:36 +0000 http://olegkutkov.me/?p=1017#comment-1400 In reply to Johannes.

Thanks!

]]>
By: Johannes https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1399 Wed, 07 Dec 2022 17:12:08 +0000 http://olegkutkov.me/?p=1017#comment-1399 In reply to Oleg Kutkov.

Hi Oleg, perfect! Thanks a lot. And all the best for your country …

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1398 Wed, 07 Dec 2022 16:44:32 +0000 http://olegkutkov.me/?p=1017#comment-1398 In reply to Johannes.

Hello! Sorry for the late reply, and thank you for your patch.
To get rid of the warning, you need to use different cxx-selected-flags.

I updated my example, so it should compile fine now

]]>
By: Johannes https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1397 Wed, 07 Dec 2022 15:45:02 +0000 http://olegkutkov.me/?p=1017#comment-1397 In reply to Johannes.

OK, I think I found it. I had to add one line in the Makefile at the end of the %.cpp.o: target in order to build the .0.cmd files. Now the complete target in my Makefile looks like this:

%.cpp.o: %.cpp
@echo $(cxx-prefix)$@
@$(HOSTCXX) $(cxxflags) -c $< -o $@
@echo -n > $$(dirname $@)/.$$(basename $@).cmd

Now I’m still seeing these compiler warnings but the Kernel Module is built and I can laod/unload it and I see the same output as you in my dmesg 🙂

]]>
By: Johannes https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1396 Fri, 02 Dec 2022 16:31:39 +0000 http://olegkutkov.me/?p=1017#comment-1396 Hi Oleg,

thanks for your tutorial. I tried to follow it but I don’t get it compiled with Ubuntu 22.10.

First I had to change some minor things i.e. #include <stdarg.h> => #include <linux/stdarg.h>
And I think in

void *operator new(size_t sz) throw ()
{
kmalloc(sz);
return kcmalloc(sz);
}

the line calling kmalloc(sz); is not valid anymore and should be removed?

But my biggest problem is that I wnd up with a linker error. Here’s the complete output on my test machine:

clean
make -C /lib/modules/5.19.0-26-generic/build M=/home/johannes/KernelCPP clean
make[1]: Entering directory ‘/usr/src/linux-headers-5.19.0-26-generic’
make[1]: Leaving directory ‘/usr/src/linux-headers-5.19.0-26-generic’
building module
make -C /lib/modules/5.19.0-26-generic/build M=/home/johannes/KernelCPP modules
make[1]: Entering directory ‘/usr/src/linux-headers-5.19.0-26-generic’
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] /home/johannes/KernelCPP/module.o
CC [M] /home/johannes/KernelCPP/kern_lib.o
CC [M] /home/johannes/KernelCPP/logger.o
g++ [M] /home/johannes/KernelCPP/cpp_support.cpp.o
cc1plus: warning: ?-Werror=? argument ?-Werror=strict-prototypes? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=implicit-function-declaration? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=implicit-int? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=incompatible-pointer-types? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=designated-init? is not valid for C++
cc1plus: warning: command-line option ?-std=gnu11? is valid for C/ObjC but not for C++
g++ [M] /home/johannes/KernelCPP/cpp_module.cpp.o
cc1plus: warning: ?-Werror=? argument ?-Werror=strict-prototypes? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=implicit-function-declaration? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=implicit-int? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=incompatible-pointer-types? is not valid for C++
cc1plus: warning: ?-Werror=? argument ?-Werror=designated-init? is not valid for C++
cc1plus: warning: command-line option ?-std=gnu11? is valid for C/ObjC but not for C++
LD [M] /home/johannes/KernelCPP/cpp_kernel.o
MODPOST /home/johannes/KernelCPP/Module.symvers
/home/johannes/KernelCPP/.cpp_support.cpp.o.cmd: No such file or directory
make[2]: *** [scripts/Makefile.modpost:128: /home/johannes/KernelCPP/Module.symvers] Error 1
make[1]: *** [Makefile:1765: modules] Error 2
make[1]: Leaving directory ‘/usr/src/linux-headers-5.19.0-26-generic’
make: *** [Makefile:44: cpp_kernel.ko] Error 2

Those .*.o.cmd files seem to be built only for c sources?

Any idea what I could do here?

Thanks and best regards,
Johannes

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1360 Mon, 15 Aug 2022 18:32:08 +0000 http://olegkutkov.me/?p=1017#comment-1360 In reply to Pat.

Hello. Make sure that you have your Linux kernel headers installed.
Check your running kernel version with uname -r and install the headers package according to your distributive.

]]>
By: Pat https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1359 Mon, 15 Aug 2022 17:26:31 +0000 http://olegkutkov.me/?p=1017#comment-1359 Hello,
Thanks for the tutorial, it’s super nice.
I followed your tutorial, I had at the end the list of the following files:

cpp_kernel.cpp
cpp_module.cpp
cpp_module.h
cpp_support.cpp
kern_lib.c
kern_lib.h
kern_log.c
logger.h
Makefile

When I run the command: “make clean && make”

I have the following error:

clean
make -C /lib/modules/5.10.0-16-amd64/build M=/home/mrzk/src/kernel_c_cpp clean
make[1]: enter the directory “/usr/src/linux-headers-5.10.0-16-amd64”
make[1]: leave the directory “/usr/src/linux-headers-5.10.0-16-amd64”
building-module
make -C /lib/modules/5.10.0-16-amd64/build M=/home/mrzk/src/kernel_c_cpp modules
make[1]: enter the directory “/usr/src/linux-headers-5.10.0-16-amd64”
make[3]: *** No rule to make target ‘/home/mrzk/src/kernel_c_cpp/module.o’, needed for ‘/home/mrzk/src/kernel_c_cpp/cpp_kernel.o’. Stop.
make[2]: *** [/usr/src/linux-headers-5.10.0-16-common/Makefile:1846:/home/mrzk/src/kernel_c_cpp] Error 2
make[1]: *** [/usr/src/linux-headers-5.10.0-16-common/Makefile:185:__sub-make] Error 2
make[1]: leave the directory “/usr/src/linux-headers-5.10.0-16-amd64”
make: *** [Makefile:44:cpp_kernel.ko] Error 2

]]>
By: Oleg Kutkov https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1042 Tue, 18 May 2021 16:56:13 +0000 http://olegkutkov.me/?p=1017#comment-1042 In reply to Linux kernel monule on c++ – Windows Questions.

Hello. Can you share details of what’s not working?

]]>
By: Linux kernel monule on c++ – Windows Questions https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/#comment-1041 Tue, 18 May 2021 16:39:56 +0000 http://olegkutkov.me/?p=1017#comment-1041 […] day. I need to write linux kernel module using c++. I’ve found this tutorial https://olegkutkov.me/2019/11/10/cpp-in-linux-kernel/, but code from it doesn’t work ;( I’d like to find any example of c++ kernel module […]

]]>