Can a kext link with a static lib file .a?

We have a main kext project(as A for short), also we have another project(B) mostly provide the basic utilitis to the main kext project A. We want to build the B as a static library file B.a, then add the B.a to the A project.


But seems it doesn't work: first we can't build the B.a project (it's a kext project as origin). So my question is, for this case, do we need to create a framework from B and use it in A? can a kext link with a static lib file?


@eskimo, any comments?

Accepted Reply

I’ve never done this myself but I suspect it’s feasible. The tricky thing here is that KEXT targets build with special flags (for example,

-mkernel
, but that’s just one of many) that get the code to build in kernel mode, and you need to build your static library the same way. If you don’t, you’ll end up trying to load user-space-built code into the kernel, which is going to end badly.

My trick for this sort of thing is to create a dummy project in Xcode, build it, and then look at the build transcript to see how Xcode invoked the compiler and linker. In this case you’d need to do that twice, once for a KEXT and another time for a static library. Once you’ve isolated the KEXT-relevant flags in the former, you can copy them over to the latter.

Honestly, if I were you, I’d just include your utilities library as source as part of your main KEXT target. Building C code is pretty darned fast, and Xcode is pretty good about only rebuilding code that’s changed, so a static library will only speed up the initial build, and I don’t think that’s worth the extra complexity.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I’ve never done this myself but I suspect it’s feasible. The tricky thing here is that KEXT targets build with special flags (for example,

-mkernel
, but that’s just one of many) that get the code to build in kernel mode, and you need to build your static library the same way. If you don’t, you’ll end up trying to load user-space-built code into the kernel, which is going to end badly.

My trick for this sort of thing is to create a dummy project in Xcode, build it, and then look at the build transcript to see how Xcode invoked the compiler and linker. In this case you’d need to do that twice, once for a KEXT and another time for a static library. Once you’ve isolated the KEXT-relevant flags in the former, you can copy them over to the latter.

Honestly, if I were you, I’d just include your utilities library as source as part of your main KEXT target. Building C code is pretty darned fast, and Xcode is pretty good about only rebuilding code that’s changed, so a static library will only speed up the initial build, and I don’t think that’s worth the extra complexity.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

After some pratice, the answer is yes. But we need to take care to build the static lib with kernel stuff such as kernel framework and header files which is something tricky a little bit for new hand...