简体   繁体   English

为什么我们将 C++ 类型定义为 LLVM 中的 Objective-C 编译器的 void(而 C++ 编译器可以看到 Objective-C 接口)?

[英]Why do we define C++ types as void for the Objective-C compiler in LLVM (while it is okay for the C++ compiler to see objective-C interfaces)?

I was going through the source of an iOS project that uses a C++ library when I stumbled upon this few rather interesting lines of code in the header file of a Objective-C wrapper for C++:当我在 C++ 的 Objective-C 包装器的头文件中偶然发现这几行相当有趣的代码行时,我正在浏览一个使用 C++ 库的 iOS 项目的源代码:

#ifdef __OBJC__
#ifndef __cplusplus
typedef void VisageTracker;
typedef void FaceData;
typedef void DemoFrameGrabber;
typedef void FDP;
typedef void VsImage;
#endif
#endif

@interface TrackerWrapper : NSObject {
CustomGLView *glView;
#ifdef __cplusplus
    VisageSDK::VisageTracker* tracker;
    VisageSDK::FaceData trackingData;
    DemoFrameGrabber *demoFrameGrabber;
    DemoObserver *demoObserver;
    int glWidth;
    int glHeight;
    bool inGetTrackingResults;
#endif
}

A question immediately hit me: Why are all the C++ types such as VisageTracker, FaceData, DemoFrameGrabber, FDP, VsImage being defined as void for the Objective-C compiler?我立刻VisageTracker, FaceData, DemoFrameGrabber, FDP, VsImage一个问题:为什么所有的 C++ 类型,如VisageTracker, FaceData, DemoFrameGrabber, FDP, VsImage被 Objective-C 编译器定义为void

I'm quite curious as how LLVM does the compilation here.我很好奇 LLVM 是如何在这里进行编译的。 It appears that the header file would be gone through twice, once by a C++ compiler and once by a Objective-C compiler.头文件似乎会经过两次,一次由 C++ 编译器处理,一次由 Objective-C 编译器处理。

Question: why must we hide the C++ types from the Objective-C compiler, but the C++ compiler is okay seeing an objective-C interface (and not only that but we can also "inject" C++ stuff into the interface)?问题:为什么我们必须对 Objective-C 编译器隐藏 C++ 类型,但 C++ 编译器可以看到 Objective-C 接口(不仅如此,我们还可以将 C++ 的东西“注入”接口中)?

Update:更新:

I tried removing these lines我尝试删除这些行

#ifdef __OBJC__
#ifndef __cplusplus
typedef void VisageTracker;
typedef void FaceData;
typedef void DemoFrameGrabber;
typedef void FDP;
typedef void VsImage;
#endif
#endif

after which the code didn't fail to compile: it appears to work just fine without these lines.之后代码并没有无法编译:它似乎没有这些行就可以正常工作。 Therefore I conclude that these lines are probably there because因此我得出结论,这些线可能在那里,因为

  1. In an older version of Clang there's a bug in the type system which can be fixed by adding these lines.在旧版本的 Clang 中,类型系统中有一个错误,可以通过添加这些行来修复。

or/and或/和

  1. It is an old tradition passed down from generation to generation with the belief that blinding the Objective-C compiler this way it can save some memories perhaps.这是一个代代相传的古老传统,相信以这种方式使 Objective-C 编译器失明或许可以节省一些记忆。

Because C++ and Objective-C is different languages.因为C++Objective-C是不同的语言。 They can be used both only in Objective-C++ .它们只能在Objective-C++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM