r/C_Programming • u/DayOk7686 • Sep 10 '26
Question OOP stracture in an lkm written in c
What is possible when it comes to using structs as classes in an lkm written in c?
For some background I usually code in c++ and very used to high(er) level code with classes and polymorphism etc. and now I'm working on a project related to the linux kernel that requires writing an lkm and for alot of things inside this lkm I would much rather working in the structure of classes and high level programming.
what is the best way to handle that sort of stuff in c programming? especially when it comes to structs
7
Upvotes
6
u/moocat Sep 10 '26
Something like this:
3
u/tastygames_official Sep 10 '26
oh look, another post in the C channel talking about trying to make C OOP. The main reason most of us use C is because we don't like the complications and performance hits that come with abstraction and polymorphism and class inheritance and OOP design patterns. So the best way to do OOP structure in C is NOT TO DO IT. Take a look at functional programming to get started.
2
u/tstanisl Sep 10 '26
Can you give some examples of constructs that you want to port to/find analog in C?
2
u/pfp-disciple Sep 10 '26 edited Sep 10 '26
The Linux kennel itself has some OOP structures. What mostly comes to mind are in (I think) the drivers, where they have the equivalent of a vtable (IIRC it's more like a defined interface). Some might consider their Intrusive List data structure as somewhat OOP.
Edit to add:
It's often very cumbersome - and I do mean very - to try to implement full OOP in C. Some parts are easier than others, some become fragile and error prone. I usually caution to consider whether a different paradigm makes more sense. If you really need an OOP feature, see whether that one feature can be achieved by itself.
1
u/Born_Location5473 Sep 11 '26 edited Sep 14 '26
The Linux driver model is an example of OOP implementation in C. There are other examples, see these:
Object-oriented design patterns in the kernel, part 1/2
https://lwn.net/Articles/444910/
https://lwn.net/Articles/446317/
Linux kernel design patterns, part 1-3
https://lwn.net/Articles/336224/
https://lwn.net/Articles/336255/
https://lwn.net/Articles/336262/
1
u/zhivago Sep 10 '26
Think about the kind of dispatch you require.
1
u/yel50 Sep 10 '26
depends on your definition of possible. Microsoft COM is basically OOP for non-OOP languages. the first c++ compiler was mainly macros. several scheme compilers generate C code, so all the goofy stuff scheme does is technically possible.
you have to understand what the compiler is actually doing in the higher level languages and implement that in c. you want to use language features that the language you're using doesn't have. either write a compiler that generates c code or do it all manually. the better option, really, is to not fight the grain and write C code in C instead of trying to write C++.
1
u/el0j Sep 10 '26
You better have no intention of ever upstreaming said module, because if you start writing it that way, it'll never be accepted.
Also; Don't.
0
u/General_Purple3060 Sep 10 '26
If you're looking for something closer to C++'s class model while still targeting C/GCC, you might want to take a look at AET.
It adds an explicit OO semantic layer to C, with
class$,interface$,extends$,implements$, virtual methods, etc., while compiling through GCC.I originally started it partly because I wanted this kind of higher-level structure without giving up C's low-level model.
0
u/HalifaxRoad Sep 10 '26
make a struct that stores all the info you would need in your "class"
Make an init function that returns a struct to initialize each instantace of your struct.
Then in the functions of your "class" you pass the instance of that struct as a pointer to each function, plus what ever other arguments you need.
Congrats now you can pretend to have objects in C.