问题描述
我想使用更多的 C 和更少的 ObjC 和 Swift 编写一个 MacOS 应用程序.我首先使用 main
函数创建了一个 main.c
文件.接下来是从 main
函数调用 NSApplicationMain
.这是在 AppKit 中定义的.但是,当我从 main.c
尝试 #include <AppKit/AppKit.h>
时出现 Could not build module 'AppKit'
错误>.我绕过了这个:
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);
这奏效了.我的问题是,很明显 NSApplicationMain
可以从 C 中调用,那么为什么我 必须 直接将其 extern 而不是直接包含 AppKit.h
?为什么他们不让您以正确的方式包含它而不是自己声明 NSApplicationMain
?我不应该这样做吗?
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?
为什么不能这样做:
#include <stdio.h>
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[]) {
return NSApplicationMain(argc, argv);
}
但是你可以这样做:
#include <stdio.h>
extern int NSApplicationMain(int argc, const char * _Nonnull *argv);
int main(int argc, const char *argv[]) {
return NSApplicationMain(argc, argv);
}
推荐答案
总之,它不是标准的 C 头文件,虽然它有 .h
扩展名.请注意,即使在 Objective-C .m
文件中,AppKit.h
也必须导入不包含
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>
这篇关于为什么我不能在纯 C 中包含 AppKit,即使我可以自己声明函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!