iPhone/iPad 项目中#import C++ 头文件的问题 [英] Problem when #import C++ Header File in iPhone/iPad Project
问题描述
我有一个 C++ 类,我想在 iPhone/iPad 项目中使用.
我以不同的方式创建了这个文件(比如新文件"=> C++),错误总是一样的.
当我在没有任何 #import(.h C++ 类)的情况下编译项目时,没关系.
但是一旦我#import 头文件 在我的一个标头objective-c 文件中,我就会收到如下错误:
<块引用><块引用>错误:向量:没有那个文件或目录
或
<块引用><块引用>错误:':' 标记前应有'='、'、'、';'、'asm' 或'属性'"
我尝试在文件信息中为文件类型(C++ 类)设置不同的值,在 .mm 等中重命名我的 objc 类,但它似乎不起作用.
所以我一定错过了在 objc 头文件中导入 .h c++ 类的一些东西,但是什么 :p ^^
感谢弗拉德的解决方案
1°) 包含头文件 c++ :
#ifdef __cplusplus#include "Triangulate.h"#万一
2°) 重命名 .mm 中的 objc 文件,并在他的文件信息(右击)中将文件类型设置为 sourcecode.cpp.objcpp
感谢您的帮助!
文森特
注意:Xcode 需要文件名有一个.mm"扩展名要启用的 Objective-C++ 扩展由编译器提供.
尝试在位于带有 .m
扩展名的文件中的 Objective-C 代码中使用 C++ 是最可能的问题原因,因为编译器只是无法根据错误消息识别 C++ 构造.将 .m
文件重命名为 .mm
应该会有所帮助.
更多详情,请参见将 C++ 与 Objective-C 结合使用.
假设你想在一个 Objective-C++ 源文件中使用一个 Objective-C 类,那完全没有问题.唯一的限制是您的 .h
文件必须是 Objective-C 干净的.这意味着您不能在其中使用任何 C++ 主义,或者如果您这样做,您必须将它们全部包装在 #ifdef __cplusplus 中.标头将在 ObjC 模式下编译,当它被普通的 Objective-C 文件#include 时,因此它必须消除这种情况下的任何 C++ 主义(1).所以你的 Objective-C 头文件应该包含这样的 C++ 头文件:
#ifdef __cplusplus# 包括 MyCPPHeader.h#万一
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.
When I compile the project without having any #import (of the .h C++ class), it's ok.
But as soon as I #import the header file in one of my header objective-c file, I get error like :
error: vector: No such file or directory
or
error: expected '=', ',', ';', 'asm' or 'attribute' before ':' token"
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.
So I must have missed something about importing the .h c++ class in the objc header file, but what :p ^^
SOLUTION thanks to Vlad
1°) To include the header c++ file :
#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
Thanks for helping !
Vincent
Note: Xcode requires that file names have a ".mm" extension for the Objective-C++ extensions to be enabled by the compiler.
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.
For more details, see Using C++ with 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:
#ifdef __cplusplus
# include MyCPPHeader.h
#endif
这篇关于iPhone/iPad 项目中#import C++ 头文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!