xcode iPhone/iPad 项目中#import C++ 头文件时出现的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3890552/
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
Problem when #import C++ Header File in iPhone/iPad Project
提问by Vinzius
I have a C++ class I would like to use in an iPhone/iPad project.
I created this file in different ways (like with "New File" => C++) and the error is always the same.
我有一个C++ 类,我想在 iPhone/iPad 项目中使用。
我以不同的方式创建了这个文件(比如“新文件”=> C++),错误总是一样的。
When I compile the project without having any #import (of the .h C++ class), it's ok.
当我在没有任何#import(.h C++ 类)的情况下编译项目时,没关系。
But as soon as I #import the header filein one of my header objective-c file, I get error like :
但是,一旦我在我的头目标 c 文件之一中#import 头文件,我就会收到如下错误:
error: vector: No such file or directory
错误:向量:没有这样的文件或目录
or
或者
error: expected '=', ',', ';', 'asm' or 'attribute' before ':' token"
错误:在 ':' 标记之前应为 '=', ',', ';', 'asm' 或 ' attribute'
I tried setting different values to the file type (of C++ class) in File Info, renaming my objc class in .mm etc, but it doesn't seem to work.
我尝试在文件信息中为文件类型(C++ 类)设置不同的值,在 .mm 等中重命名我的 objc 类,但它似乎不起作用。
So I must have missed something about importing the .h c++ class in the objc header file, but what :p ^^
所以我一定错过了一些关于在 objc 头文件中导入 .h c++ 类的内容,但是什么 :p ^^
SOLUTION thanks to Vlad
1°) To include the header c++ file :
解决方案感谢 Vlad
1°) 包含头文件 c++ 文件:
#ifdef __cplusplus
#include "Triangulate.h"
#endif
2°) Renaming the objc file in .mm AND in his File Info (right clic) setting file type as sourcecode.cpp.objcpp
2°) 重命名 .mm 中的 objc 文件,并在他的文件信息(右击)中将文件类型设置为 sourcecode.cpp.objcpp
Thanks for helping !
Vincent
感谢您的帮助!
文森特
回答by
Note: Xcode requires that file names have a “.mm” extension for the Objective-C++ extensions to be enabled by the compiler.
注意:Xcode 要求文件名具有“.mm”扩展名,以便编译器启用 Objective-C++ 扩展。
Trying to use C++ in Objective-C code residing in a file with .m
extension is the most probable cause of the problem because compiler just does not recognize C++ constructs according to the error message. Renaming your .m
file to .mm
should help.
尝试在驻留在带有.m
扩展名的文件中的 Objective-C 代码中使用 C++是问题的最可能原因,因为编译器只是无法根据错误消息识别 C++ 构造。将您的.m
文件重命名为.mm
应该会有所帮助。
For more details, see Using C++ with Objective-C.
有关更多详细信息,请参阅将 C++ 与 Objective-C 结合使用。
Assuming you want to use an Objective-C class in an Objective-C++ source file, there's no problem at all. The only restriction is that your .h
file must be Objective-C clean. This means that you can't use any C++-isms in it, or that if you do you must wrap them all in #ifdef __cplusplus. The header will be compiled in ObjC mode when it's #included by a plain Objective-C file, so it has to eliminate any C++isms for that situation (1). So your Objective-C header file should include C++ header like this:
假设你想在一个 Objective-C++ 源文件中使用一个 Objective-C 类,那完全没有问题。唯一的限制是您的.h
文件必须是 Objective-C 干净的。这意味着您不能在其中使用任何 C++ 主义,或者如果您这样做,您必须将它们全部包装在 #ifdef __cplusplus 中。标头将在 ObjC 模式下编译,当它被普通的 Objective-C 文件 #include 时,因此它必须消除这种情况下的任何 C++isms (1)。所以你的 Objective-C 头文件应该包含 C++ 头文件,如下所示:
#ifdef __cplusplus
# include MyCPPHeader.h
#endif