2010-01-13 48 views

回答

28

这看起来像你的预编译头文件。

预编译头文件在项目中的所有C语言文件之间共享。就好像所有的.c,.cpp,.m和.mm文件都有一个不可见的#include指令作为第一行。但Cocoa头文件是纯粹的Objective C--试图将它们包含在C/C++源代码中,除了语法错误之外,它们只会产生任何东西。因此#ifdef。

如果您的项目只包含Objective C文件(.m/.mm),这是典型情况,那么#ifdef并不是真的必要。但是,首先生成这个头文件的Xcode,也可以保护你。

即使它不是PCH文件,只有从Objective C和普通C/C++中包含该文件,该#ifdef才有意义。但不管怎样都不会伤害。

1

它只是一个宏观符号。在这种情况下,如果定义了该符号,那么程序应该导入Apple Cocoa框架(Foundation和AppKit)。

如果你正在开发一个Objective-C/cocoa应用程序,这种情况就是这种情况。换句话说,如果您正在开发C++/Carbon应用程序,那么将不会定义符号,并且这些不依赖于Objective-C的框架将不会被导入。

33

这意味着目标C编译器正在被使用。所以你可以创建混合头文件,在编译目标C或C或C++时可以使用它们。

你可以在这样的头文件中使用它,如果你想发布定义你想提供给C和C++程序员/代码的目标对象c头文件:

#ifndef MYHEADER_H 
#define MYHEADER_H 

#ifdef __OBJC__ 
// Put objective C things in this block 
// This is an objc object implemented in a .m or .mm file 
@implementation some_objc_object { 
} 
@end 
#endif 

#ifdef __cplusplus 
#define CLINKAGE "C" 
// c++ things that .m or .c files wont understand go in here 
// This class, in a .mm file, would be able to call the obj-c objects methods 
// but present a c++ interface that could be called from c++ code in .cc or .cpp 
// files 
class SomeClassThatWrapsAnObjCObject 
{ 
    id idTheObject; 
public: 
    // ... 
}; 
#endif 
// and here you can declare c functions and structs 
// this function could be used from a .c file to call to a .m file and do something 
// with the object identified by id obj 
extern CLINKAGE somefunction(id obj, ...); 
#endif // MYHEADER_H