Objective-C ++导入C ++类失败,找不到cassert [英] Objective-C++ importing C++ class fails, cassert not found

查看:3329
本文介绍了Objective-C ++导入C ++类失败,找不到cassert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在我的cocos2d + box2d项目中公开一个指向其他Objective-C ++类的Box2D(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.

UPDATE:

。是的,这是Xcode / LLVM中的一个错误。它以错误的顺序编译文件,然后覆盖其自己的设置,并打破自身。苹果的技术支持太不胜任,甚至不能理解这个问题,所以我怀疑他们很快就会修复这个bug。

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.

(注意:跳过这一节, ,对于我的原始答案,给予BRUTE FORCE但非常快速做DO解决方案)

(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吸)引用EVERY头文件 - 并为每个头文件导入...等。

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:


  1. 前往您的专案建立设定

  2. Compile Sources Assetting

  3. 将其设置为Objective-C ++

  4. 重新构建

  1. Go to build settings for your project
  2. find the "Compile Sources As" setting
  3. set it to "Objective-C++"
  4. re-build

实际的名称/值是: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆