Re: Compiling C++ modules

Alan Cox
2006-04-24T20:35:51Z

On Llu, 2006-04-24 at 13:16 -0600, Gary Poppitz wrote:
> I have the task of porting an existing file system to Linux. This
> code is in C++ and I have noticed that the Linux kernel has
> made use of C++ keywords and other things that make it incompatible.

We tried various things involving C++ along the line in kernel history
and there are so many problems it throws up the kernel took the view
that it would not use C++ in kernel. Instead object orientation is
performed more explicitly in C. This has many advantages including the
exposure of inefficient code explicitly and the avoidance of exceptions
and the assumption memory allocations just don't fail that much C++
makes without exceptions being used. It might be possible to move to a
strict C++ subset in the style of Apple but there isn't much interest in
this.

There are other problems too, notably the binary ABI between the C and C
++ compiler might not match for all cases (in particular there are
corner cases with zero sized objects and C++).

> I would be most willing to point out the areas that need adjustment
> and supply patch files to be reviewed.
>
> What would be the best procedure to accomplish this?

If you want to maintain your own out of tree file system then probably
you want to use #defines to wrap the kernel tree. Most stuff will
probably work ok if you do this although you'll want some C to C++
wrappers to interface to the kernel obviously.

If you want to submit the file system to the kernel source then it needs
shifting from C++ to C but of course there are people in the community
who can help you. As the kernel C is very object based that isn't
usually too much of a problem. Most objects in the kernel (inodes, files
etc) are of the form

struct thing {
struct thing_ops *ops; /* methods */
blah
}

and that style fits much C++ code being ported over.

There are a few anti C++ bigots around too, but the kernel choice of C
was based both on rational choices and experimentation early on with the
C++ compiler.

Alan