Re: c++ module support?

Andi Kleen (ak@muc.de)
25 Mar 1998 06:31:07 +0100


James Mastros <root@jennifer-unix.dyn.ml.org> writes:

> On 24 Mar 1998, Neal Becker wrote:
> > Can kernel modules be written in c++? Are there any known
> > restrictions? (Other than the init_module must be declared extern C).
>
> The kernel (and insmod) don't care what language the module is written in
> (as far as it knows, you could have typed the bytecode into a hex-editor)
> -- so long as it is validly executable bytecode, and all data passed back
> and forth is in the format that the kernel expects it (note that all
> functions that you give out pointers to are going to need to be extern c.)

No, g++ uses normal C calling conventions, so there is no need for extern "C"
if you use pointers [only methods will expect a this arguments]. extern "C"
is only needed if you want to _link_ with C or C++ code, but not if you
pass pointers.

Caveats:

- You might have a hard time to compile all needed kernel includes with g++.
- You need to wrap the kernel includes with extern "C" {
...
}
to link with the kernel.
- Linux kernel includes depend heavily on inline assembler and volatile
semantics. Earlier versions of g++ were known to have bugs in this regard
(ignoring volatile in many cases etc.). If the g++ version you use does this
you might get anything between subtly wrong code to all break loose @)
- C++ depends more on the runtime library (for new, exceptions etc.). You
can't use the normal libgcc2 or libstdc++, and have to reimplement the
needed runtime routines to work in the kernel environment.

-Andi

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu