Is it possible to use C++ in a Kernel Extension?

In the "Kernel Extension Programming Topics" document there are two examples of kernel extensions - HelloKernel and Hello I/O Kit.

The Hello IO Kit is created as an I/O Kit Driver project and HelloKernel as a kext project.

Hello I/0 Kit has C++ classes in it whereas HelloKernel is just C.

Is it possible to use C++ in a kext project? If so what header files or bundles must be included to make it possible?

My aim is to develop an NKE (which I presume has to be created as a kext project) and I would rather implement it it using classes than using just C.

iMac, Mac OS X (10.4.11)

Posted on Jan 28, 2010 9:01 AM

Reply
3 replies

Jan 28, 2010 11:33 AM in response to etresoft

Thanks.
Was it pre-Snow Leopard?

I've just seen this following text from the Kernel release notes for Snow Leopard, would this make a difference? ( I notice it mentions I/O however, why is C++ allowed in I/O kexts but not in others?)

*Mixed C/C++ Kexts*
Mac OS X Snow Leopard features support for kexts that use both C and C++ code. On prior
releases of Mac OS X, making use of Libkern C++ classes in a generic (C-based) kext was a
risky proposition, since the I/O Kit auto-unloading mechanism destroyed any kext
referencing such classes if there were no instances of them. Apart from that, even if non-
Libkern C++ was used, the stub routine invoked at unload called the kext’s static C++
destructors before the kext’s stop routine, and when the kext was auto-unloaded the stop
routine wasn’t invoked at all.
Snow Leopard corrects these issues by:
Marking only kexts that define subclasses of IOService for auto-unload. Non-I/O Kit
kexts loaded on Snow Leopard can safely use and subclass Libkern C++ classes.
Altering the stub unload routine to call the kext’s stop routine first, then the kext’s
static C++ destructors. Important: To gain this behavior you must build your kext to
be targeted against Snow Leopard, as the stub routine is built into the kext.
Always unloading a kext by calling both the stop routine and then the C++ static
destructors, regardless of kext type (generic or I/O Kit).

Jan 28, 2010 11:51 AM in response to hotcheese

hotcheese wrote:
Was it pre-Snow Leopard?


Yes

I've just seen this following text from the Kernel release notes for Snow Leopard, would this make a difference? ( I notice it mentions I/O however, why is C++ allowed in I/O kexts but not in others?)

*Mixed C/C++ Kexts*
Mac OS X Snow Leopard features support for kexts that use both C and C++ code. On prior
releases of Mac OS X, making use of Libkern C++ classes in a generic (C-based) kext was a
risky proposition, since the I/O Kit auto-unloading mechanism destroyed any kext
referencing such classes if there were no instances of them. Apart from that, even if non-
Libkern C++ was used, the stub routine invoked at unload called the kext’s static C++
destructors before the kext’s stop routine, and when the kext was auto-unloaded the stop
routine wasn’t invoked at all.
Snow Leopard corrects these issues by:
Marking only kexts that define subclasses of IOService for auto-unload. Non-I/O Kit
kexts loaded on Snow Leopard can safely use and subclass Libkern C++ classes.
Altering the stub unload routine to call the kext’s stop routine first, then the kext’s
static C++ destructors. Important: To gain this behavior you must build your kext to
be targeted against Snow Leopard, as the stub routine is built into the kext.
Always unloading a kext by calling both the stop routine and then the C++ static
destructors, regardless of kext type (generic or I/O Kit).


That is very interesting. I suggest you post your question on Apple's Darwin kernel mailing list. I have seen postings there that both echo what I told you and claim that you can make C++ work fine. It is highly likely that I just need more experience writing kernel code (and a better project idea). I would like to know the answer to this question myself. It doesn't seem as cut and dried anymore.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Is it possible to use C++ in a Kernel Extension?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.