r/C_Programming • • 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

19 comments sorted by

6

u/moocat Sep 10 '26

Something like this:

#include <stdio.h>

struct Moo;

struct MooVtable {
    int (*add)(const struct Moo*, int);
    void (*set)(struct Moo*, int);
};

struct Moo {
    const struct MooVtable *vtable;
    int value;

};

int Add(const struct Moo* moo, int other) {
    return moo->value + other;
}

void Set(struct Moo* moo, int new) {
    moo->value = new;
}

const struct MooVtable moo_vtable = {
    .add = &Add,
    .set = &Set,
};


int main() {

    struct Moo adder = {
        .vtable = &moo_vtable,
        .value = 0,
    };

    printf("%d\n", adder.vtable->add(&adder, 5));
    adder.vtable->set(&adder, 8);
    printf("%d\n", adder.vtable->add(&adder, 5));
}

1

u/DayOk7686 Sep 10 '26

Thank you very much
I will almost certainly return to this comment in the future

-1

u/imaami Sep 10 '26

Use static. Define static const struct MooVtable moo_vtable and also make the Add() and Set() functions static. None of those need to be exported, assuming they're only ever called via the vtable pointers.

On the other hand, that entire vtable thing is an antipattern in C. Completely useless. Adds both overhead and makes the code more verbose.

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.

4

u/DayOk7686 Sep 10 '26

Trust me. If I could code lkms in c++ i would

4

u/tastygames_official Sep 10 '26

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.

5

u/[deleted] Sep 10 '26

[removed] — view removed comment

1

u/tastygames_official Sep 10 '26

doesn't everyone just use macros? ;-)

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

 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 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.

1

u/realdreamer1993 Sep 11 '26

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 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.