所以我想在我的 cocos2d + box2d 项目中公开一个 Box2D (C++) 指向其他 Objective-C++ 类的指针.我在我的接口中声明了一个方法getWorld",它引用了 C++ 类 b2World 并导入了 Box2D.h.我项目中的所有文件都是 .mm 扩展名,我得到以下编译错误:
So I want to publicly expose a Box2D (C++) pointer to other Objective-C++ classes in my cocos2d + box2d project. I declare a method "getWorld" in my interface which references C++ class b2World and imports Box2D.h. All the files in my project are .mm extension, and I get the following compile error:
In file included from DebugDrawLayer.mm:2:
In file included from World.h:10:
In file included from external/Box2d/Box2D/Box2D.h:34:
external/Box2d/Box2D/Common/b2Settings.h:22:10: fatal error: 'cassert' file not found
#include <cassert>
我猜 Box2D.h 不知何故被编译为 C 而不是 C++,但我不明白这是怎么发生的.根据日志,包含链显然是从 .mm 文件开始的.
I guess Box2D.h is somehow compiled as C instead of C++, but I fail to understand how this happens. According to the log the include chain clearly starts from a .mm file.
更新:
日志显示 World.mm(之前编译)清楚地编译为 Objective-c++
The log says for World.mm (compiled earlier) that it clearly compiles as objective-c++
CompileC Objects-normal/i386/World.o World.mm normal i386 objective-c++ com.apple.compilers.llvm.clang.1_0.compiler
但是对于 DebugDrawLayer.mm 它说的是objective-c
but for DebugDrawLayer.mm it says objective-c
CompileC Objects-normal/i386/DebugDrawLayer.o DebugDrawLayer.mm normal i386 objective-c com.apple.compilers.llvm.clang.1_0.compiler
这两个文件都设置为默认值 - Objective-C++ 源代码.什么给了..?
Both files are set as default - Objective-C++ source. What gives..?
这似乎是 Xcode4 中的一个主要错误,与编译器设置无关 - 只是 Xcode 内部.
This appears to be a MAJOR bug in Xcode4, nothing to do with compiler settings - just Xcode internals.
更新:
我最终找到了根本原因.是的,这是 Xcode/LLVM 中的一个错误.它以错误的顺序编译文件,然后覆盖自己的设置,并自行中断.苹果的技术支持太无能,甚至无法理解这个问题,所以我怀疑他们很快就会修复这个错误.
I eventually found the root cause. Yes, it's a bug in Xcode/LLVM. It compiles files in the wrong order, and then overrides its own settings, and breaks itself. Apple's tech support was too incompetent to even understand the problem, so I doubt they'll be fixing the bug anytime soon.
(注意:跳过此部分,并在下面查看我的原始答案,这给出了一个蛮力但非常快速的解决方案)
(NB: Skip this section, and see below, for my original answer, that gives a BRUTE FORCE but VERY QUICK TO DO solution)
会发生什么......如果编译器看到"一个 C++ 头文件在处理一个 C 类......然后它(内部)将该头文件标记为C"(即使这是几乎不可能).
What happens is ... if the compiler "sees" a C++ header while processing a C class ... it then marks that header (internally) as "C" (even when this is literally impossible).
稍后,当它回到那个头文件,尝试用 C++ 编译它时,它发现它已经告诉自己头文件是C"(因为它非常愚蠢)......并且立即崩溃.
Later on, when it comes back to that header, trying to compile it with C++, it finds it has already told-itself that the header is "C" (because it's incredibly stupid) ... and promptly crashes.
正确解决方案(需要大量时间和精力)是检查项目中的每个 C 文件,并检查(手动,因为 Xcode 很烂)对每个头文件的每个引用 --并且,对于每一个,它导入的每个头文件......等等.
The correct solution (which requires lots of time and effort) is to go through EVERY C FILE in your project, and check (manually, because Xcode sucks) EVERY reference to EVERY header file -- and, for each one, EVERY header file it imports ... etc.
(这可能需要几个小时)
(this can take hours)
...直到找到导致 C 文件看到"C++ 标头的导入链.
...until you find the chain of imports that leads to a C file "seeing" a C++ header.
所有这些都应该是自动化的(但不是).并且根本问题不应该发生(如果 Xcode 编写正确的话).
All of this should be automated (but isn't). And the root problem shouldn't happen (if Xcode were correctly written).
我尝试了网络上其他地方列出的所有方法,大多数解决方案都是不断删除/添加文件,如果幸运的话,Xcode 最终会自行修复".
I tried everything listed elsewhere on the web, most of the solutions are "keep removing/adding the file, and eventually Xcode will fix itself if you're lucky".
我不走运.唯一有效的是:
I wasn't lucky. The only thing that worked was:
实际的名称/值是:GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp" - 所以我的猜测是这回避了 Xcode(坏了!)的内部逻辑.
The actual name/value is: "GCC_INPUT_FILETYPE = sourcecode.cpp.objcpp" - so my guess is that this sidesteps Xcode's (broken!) internal logic.
这篇关于Objective-C++ 导入 C++ 类失败,未找到 cassert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!