stlmacos-catalinaxcode11.4driverkitmacos-system-extension

Can I use STL in a DriverKit driver?


Can I use for example std::vector in DriverKit driver in macOs / XCode?

DriverKit has some container class like OSArray https://developer.apple.com/documentation/driverkit/osarray?language=objc

If I create a new "DriverKit driver" project and include <vector> then I get build errors. Those error comes from including <cstring> and the error is

No member named 'strcpy' in the global namespace

No member named 'strcat' in the global namespace


Solution

  • As far as I can tell, you aren't supposed to. The headers you end up pulling in there aren't from the DriverKit SDK, they're the default ones that come with Xcode's compiler toolchain. They assume a normal macOS build environment, not the dext environment. The DriverKit SDK doesn't include C++ stdlib headers, and only contains some stripped-down headers for a subset of the C standard library. Hence the missing strcpy and strcat.

    Linking against libc++ also fails, even the version included in the DriverKit SDK. I don't know why there is a version of that library included with the DriverKit SDK, but it's clearly not intended for being used in dexts.

    There's presumably nothing in particular stopping you from including some other container library, or even directly including parts of an STL implementation. You may need to manually wire up memory allocation calls though.