C++ code in a kernel module?

M. Warner Losh imp at bsdimp.com
Mon Sep 8 17:01:45 PDT 2003


In message: <DBE1CF41-E222-11D7-80D8-0003930719D8 at colorado.edu>
            John Giacomoni <John.Giacomoni at colorado.edu> writes:
: How would one go about creating a kernel module which utilizes
: C++  code?

That's a tough row to hoe.

: I was planning on using the macro __cplusplus to toggle using
: extern "C" { }, however the bsd.kmod.mk style Makefiles seem to
: force the language to -std=c99 even when compiling with c++ .

Sounds like a bug to me.

: my initial steps have been as follows:
: take a functioning C based kernel module and rename to .cc
: added extern "C" around the includes.
: #defined key words such as new to xxx_new
: recompiled the new .cc file by hand without -std=c99, but
: keeping all the flags as the Makefile set them.
: then linked using the Makefile and finally loaded the module.

Boom!  That was the big booming sound I heard earlier in the day.

: as long as I do not make any calls into kernel functions
: things seem to work (load), however when I make a call to
: mtx_init then on load i get the following error:
: __gxx_personality_v0 undefined.

You will need to add all the runtime support.  You will need to
compile w/o excetions in all likelihood, since it requires DWARF
support, iirc.  You may also to disable RTTI support.  Templates may
work, as long as they don't need run time linker support, best to
avoid them.

I don't know about __gxx_personaility_v0, but your best bet is to look
at the .o's and find where it is referenced.  Then back track it to
what function, then to what construct and go from there.

There be dragons here.  You are about to become very familiar with the
version of g++ that you are working with as well as subtle variations
that happen with different compiler flags.  I've tried it in the past
and there are lots of issues.

Warner


More information about the freebsd-hackers mailing list