是否有像 __APPLE__ 这样的定义用于 Objective C/C++

Mar*_*ott 2 c++ xcode objective-c c-preprocessor

我得到了一个小的 C++ 帮助文件,它有一些 std::strings 的支持函数。当我在“.cpp”文件和“.mm”文件中使用它时,我想添加一些 Objective C++ 帮助函数。现在我搜索如下定义:

#ifdef __APPLE__
#ifdef __I_AM_INCLUDED_IN_A_MM_FILE__
// define some Objective C++ code here
#endif
#endif
Run Code Online (Sandbox Code Playgroud)

有定义吗?是否有 XCode 定义的列表?

Pau*_*l R 6

__OBJC____OBJC2__在编译 .mm 文件时定义。

为了将来参考,您可以按如下方式测试此类内容:

$ clang -dM -E foo.mm > mm.txt
$ clang -dM -E foo.cpp > cpp.txt
$ sdiff -s mm.txt cpp.txt
#define IBAction void)__attribute__((ibaction)                <
#define IBOutlet __attribute__((iboutlet))                    <
#define IBOutletCollection(ClassName) __attribute__((iboutlet <
#define OBJC_ZEROCOST_EXCEPTIONS 1                            <
#define __NEXT_RUNTIME__ 1                                    <
#define __OBJC2__ 1                                           <
#define __OBJC__ 1                                            <
Run Code Online (Sandbox Code Playgroud)

(注意 foo.mm 和 foo.cpp 只是空文件)

  • 惊人的。准确命中:-) (2认同)