Xcode 中的 Objective-C++ 和 .cpp 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3698459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C++ and .cpp files in Xcode
提问by LandonSchropp
I'm trying to make a simple Objective-C++ applicaiton. All of my code is compiling fine, including the use of C++ in Objective-C classes, until I try and add a C++ class to the mix. I've created a simple C++ class:
我正在尝试制作一个简单的 Objective-C++ 应用程序。我所有的代码都编译得很好,包括在 Objective-C 类中使用 C++,直到我尝试将 C++ 类添加到组合中。我创建了一个简单的 C++ 类:
Test.h
测试.h
class Test {
};
and included this file in a Objective-C class (with a .mm extension) and I get the following build error:
并将此文件包含在 Objective-C 类中(带有 .mm 扩展名),我收到以下构建错误:
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Test'
Clearly I'm missing some simple concept here. I'd appreciate some enlightment.
显然我在这里遗漏了一些简单的概念。我很感激一些启示。
采纳答案by Pavel
helixed's answer will not help, your class will be just skiped by preprocessor if __cplusplus undefined.
helixed 的回答无济于事,如果 __cplusplus 未定义,您的课程将被预处理器跳过。
Most of all you trying to include C++ class from *.m file, try to rename it to *.mm. This solve the same problem on my side.
最重要的是,您尝试从 *.m 文件中包含 C++ 类,尝试将其重命名为 *.mm。这在我这边解决了同样的问题。
回答by LandonSchropp
Well, after scanning more closely over Apple's Documentation, it looks like the answer to to use the __cplusplus
preprocessor flag in the header file. Here's what the code looks like now:
好吧,在更仔细地扫描Apple 的 Documentation 之后,它看起来像是__cplusplus
在头文件中使用预处理器标志的答案。下面是代码现在的样子:
#ifdef __cplusplus
class Test {
};
#endif
回答by deceleratedcaviar
Shouldn't the preprocessor command required be:
不应该需要的预处理器命令是:
#ifdef __cplusplus
回答by sbsmith
In Xcode 6 there is the following option:
在 Xcode 6 中有以下选项:
Apple LLVM 6.0 - Language -> Compile Sources As
You can set that to Objective-C++ and then the file extension does not matter. This is particularly useful if you are working cross-platform and need to use other file extensions.
您可以将其设置为 Objective-C++,然后文件扩展名无关紧要。如果您正在跨平台工作并且需要使用其他文件扩展名,这将特别有用。