19

When I develop a loadable kernel module (LKM) should I use C?

Is it possible to develop a loadable kernel module (LKM) on Linux with language other than C for example C++?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137

5 Answers5

17

It may be possible to an extent, but be warned (from http://www.tux.org/lkml/#s15-3):

Is it a good idea to write a new driver in C++? The short answer is no, because there isn't any support for C++ drivers in the kernel.

Why not add a C++ interface layer to the kernel to support C++ drivers? The short answer is why bother, since there aren't any C++ drivers for Linux.

I think the best idea is to consult existing resources (there are a few kernel driver books, including a free one online) which are all in C, get the basics figured out, then you can try and see if you can get c++ to work there.

But I very much doubt that will be easy. You don't even have access to the full C standard library in the kernel. Something to think about: it does not link to shared libraries, and the total executable image is usually 2-3 MB.

CodeClown42
  • 11,194
  • 1
  • 32
  • 67
  • 19
    In addition to this warning, remember that there is no way your driver will be accepted into Linus' tree if it's not in C. – Kristof Provost May 06 '12 at 08:56
  • 6
    Program written C++ does not require any shared libraries and can work without standard library and even without malloc. We can look at C++ in role of system language as an extension of C. Better syntax, OOP. I, personally, wrote much code in mixed C/C++ for bare-metal micro-controllers and all work very good. So I believe there is no problem with C++ language, the problem is with compiler and `extern "C" {}` around every Linux header. – kyb Sep 10 '19 at 09:44
6

I'm pretty sure I saw a kernel configuration option somewhere allowing C++ in kernel modules, a while back (but cannot find it again). I can see how certain templates would be very interesting to use in driver modules. Just for anecdotics: the OS X Mach kernel is partly written in C++.

RJVB
  • 698
  • 8
  • 18
4

In the end it comes down to object code, which is generated by the Assembler and linked together afterwards.

So yes it is possible, you find a debate about that here.

It depends on what you want to do with the LKM, do you want to use it for yourself and some experiments or is it going to become productive anywhere?

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
2

There is an operating system which is rewriting Linux Kernel in C++ it's called Boss-Mool and you can write drivers using C++. Here's the link : https://www.bosslinux.in/boss-mool

-8

Well, the original question was for Linux, not OS X or Windows or whatever.

There is absolutely no way to write a Linux kernel mode driver in C++ ! That's because you would need to link with libstdc++ which will not link with your module. libstdc++ is not available for kernel mode, as simple as that !

Alexey Polonsky
  • 1,141
  • 11
  • 12
  • 11
    This adds very little to what is already here about the poor wisdom of doing so, but it is additionally technically mistaken, as the necessary support can be included within the module itself, just as C++ can be used for embedded targets that do not support shared libraries at all. The authors of the other answers took that into account. – Chris Stratton Mar 03 '14 at 19:50