Search code examples
cmacoscocoaappkit

Why I cannot include AppKit in pure C, even though I can declare the functions myself?


I would like to write a MacOS application using more C and less ObjC and Swift. I started by creating a main.c file with the main function. Next is to call NSApplicationMain from the main function. This is defined in AppKit. However, I get a Could not build module 'AppKit' error when I try #include <AppKit/AppKit.h> from main.c. I circumvented this with:

extern int NSApplicationMain(int argc, const char * _Nonnull *argv);

This worked. My question is, clearly NSApplicationMain can be called from C so why do I have to extern it directly instead of including AppKit.h directly? Why they do not let you include it the proper way instead of declaring NSApplicationMain yourself? Am I not supposed to do that?

Why can you not do this:

#include <stdio.h>
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[]) {
    return NSApplicationMain(argc, argv);
}

But you can do this:

#include <stdio.h>
extern int NSApplicationMain(int argc, const char * _Nonnull *argv);
int main(int argc, const char *argv[]) {
    return NSApplicationMain(argc, argv);
}

Solution

  • In short, it is not standard C header file though it has .h extension. Note, that even in Objective-C .m files AppKit.h must be imported not included

    #import <AppKit/AppKit.h>