将 C++ 和 Objective C 包含到 Swift 框架中

Include C++ and Objective C into Swift Framework

我正在尝试构建一个 Swift 框架,其中包括用于 iOS

的 C++ 和 OpenCV 框架

据我了解,我们不能在框架中建立桥接 header,我们必须处理保护伞 header。

我的问题是 XCode 没有将 C++ 文件作为 C++ 处理,出现了那种编译器错误:
Unknown type name 'class'; did you mean 'Class'? 它也找不到 <iostream>

我目前有我的保护伞,其中包括我的 C++ 文件的 objc 包装器。
正确的做法是什么?

在我的框架保护伞下header

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#else
    #ifndef FOUNDATION_EXPORT
        #if defined(__cplusplus)
            #include <iostream>
            #define FOUNDATION_EXPORT extern "C"
        #else
            #define FOUNDATION_EXPORT extern
        #endif
    #endif
#endif

#import "AGImageDescriptor.h"
#import "AGKitLib.h"
#import "Detector.h"
#import "Observable.h"
#import "Shooter.h"
#import "DocumentDetectedState.hpp"
#import "DocumentMatchState.hpp"
#import "GoodPerspectiveState.hpp"
#import "GoodPositionState.hpp"
#import "InactiveState.h"
#import "ReadyState.hpp"
#import "StartState.h"
#import "State.h"
#import "States.h"
#import "TextDetectedState.hpp"
#import "Tools.hpp"
#import "OpenCVWrapper.h"
#import "UIImage+OpenCV.h"

FOUNDATION_EXPORT double AGKitScanVersionNumber;
FOUNDATION_EXPORT const unsigned char AGKitScanVersionString[];

我真的不是 C++ 专家也不是 C 专家所以我可能在这里遗漏了一些东西。

您收到的错误表明您的保护伞 header 可能直接或间接包含 C++ 代码。其实我看到很多.hpp个文件,大概是C++的header个吧。在框架中存在 Swift 代码的情况下,您不能这样做。所有 C++ 代码必须限制在 Objective-C++ 包装器的实现文件中(.mm 扩展,而不是 .m 用于 Objective-C 文件)。 Objective-C++ (.mm) 文件可以混合使用 C++ 和 Objective-C 代码,并包含必要的 C++ headers。

这里有一些 questions/answers 可能会有所帮助:Unknown type name 'class'; did you mean 'Class'? and Video processing with OpenCV in IOS Swift project