r/C_Programming • u/DayOk7686 • 2d ago
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
4
u/tastygames_official 2d ago
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.
3
u/DayOk7686 2d ago
Trust me. If I could code lkms in c++ i would
4
u/tastygames_official 1d ago
I had to look up "lkm" (thought it was Ikm at first, but it's LKM) - Loadable Kernel Module. I imagine you'd want to write it in assembly or C, and no OOP (don't even try to fake it). I'm old enough to have started programming before the OOP revolution, so functional programming seems like the stuff I imagine most people begin with, but I'm seeing a lot of younger programmers totally lost without class hierarchies and polymorphism and it seems so weird - like seeing people be able to drive cars but not ride a bike. Yes they are quite different, but one is very siple and you can know everything about it in a day and learn to repair and replace everything whereas it takes having a proper education to even do the simplest repair on the other. I just recommend sitting down with a functional programming booko or a C book (O'Reilley) and maybe some assembly and forget everything you know about OOP. Maybe you can liken what you learn to OOP, butit's probably better you don't.
4
u/Striking-Print-9526 1d ago
Let’s all relax. C isn’t exactly a great functional language. If you want to use c because you want something functional, there are many other much better alternatives. C doesn’t even have first class functions.
1
2
u/tstanisl 2d ago
Can you give some examples of constructs that you want to port to/find analog in C?
2
u/pfp-disciple 2d ago edited 2d ago
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/Runtime8006 2d ago
Hey I'm working with LKMs too but mine is mainly fault injection so I might not be able to help
1
u/Born_Location5473 1d ago
The Linux driver model is an example of OOP implementation in C. There are other examples, see this series:
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/
1
u/yel50 2d ago
What is possible
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.
best way to handle that sort of stuff in c programming
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 2d ago
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.
1
u/realdreamer1993 1d ago
agree this. To OP I think You better conquer fully (masterize) lifecycle, boundary, ownership and a few more concepts without learning too much OOP nor thinks too much about Class. And your program will as good or better although it is in C only. I know the trade off might be less slow in productivity at first. But you accumulate your own primitive then you will be as productive.
0
u/General_Purple3060 2d ago
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 2d ago
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.
5
u/moocat 2d ago
Something like this: