Building an Apple OSX Kernel Module With CMake – C/C++

If you google around about how to build an osx kext you will find very few results, a few email messages saying not to bother or that it it impossible.

So what’s all the voodoo about.  A few defines, some linker options, some compiler options. Also a special info.c file and Info.plist.

Note: if you want to sign your kernel module you will need to apply to apple for a special kernel module code signing certificate.

Setup basic cmake project. We need 3.8 or newer to use BUNDLE_EXTENSION for the .kext bundle type.

Setup some variables for later. These are both optional if you use the default system sdk or you don’t want to code sign.

Add a debug preprocessor def. This is optional.

Add the special preprocessor defines to access internal kernel structures.

Add include directories for osx kernel headers and private headers.

Add an executable bundle target.

Set the target bundle extension to “kext” and the plist file.

Set a million compile options.  These were taken from a minimal xcode project.

Note: The -mmacosx-version-min=10.10 can be removed or changed to support older versions of OSX.

 

Add the libraries required for the kernel module and linker options.

Note: The -mmacosx-version-min=10.10 can be removed or changed to support older versions of OSX.

Add a custom target to sign the kernel module.  This can be skipped if code signing isn’t needed.

The example.c file:

The example_info.c File.  This is usually generated by xcode during the build process.

It just sets the main entry points to example_start and example_stop

 

The complete cmake file.

The magic Info.plist file.  All references to “example” will need to match the binary and com.example items.

 

GitHub Repository with the example.

 

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisShare on RedditShare on StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *