我是 Swift 的新手。最近遇到一个问题 有一个静态库编译源作为 Objective-C++。例如,在 .h 文件中“导入”,如:
#import <Foundation/Foundation.h>
#import <map>
@interface CppLib : NSObject
@end
现在我创建一个新的swift 项目,包括由静态库项目创建的.a 和.h 文件。添加一个名为“Swift_CppLib-Bridging-Header.h”的新桥接文件:
#ifndef Swift_CppLib_Bridging_Header_h
#define Swift_CppLib_Bridging_Header_h
#import "CppLib.h"
#endif
然后CMD+B,构建项目失败:
/Users/air/Somnus/CodeDemo/Swift_CppLib/Swift_CppLib/Swift_CppLib-Bridging-Header.h:12:9: note: in file included from /Users/air/Somnus/CodeDemo/Swift_CppLib/Swift_CppLib/Swift_CppLib-Bridging-Header.h:12:
#import "CppLib.h"
^
/Users/air/Somnus/CodeDemo/Swift_CppLib/Swift_CppLib/Lib/CppLib.h:10:9: error: 'map' file not found
#import <map>
^
<unknown>:0: error: failed to import bridging header '/Users/air/Somnus/CodeDemo/Swift_CppLib/Swift_CppLib/Swift_CppLib-Bridging-Header.h'
谁能帮我解决这个问题?感谢任何提示!
最佳答案
您需要确保在导出到 swift 的 header 中没有可见的 c++ 或 objective-c++。 Apple 明智地决定不支持 swift 中的 c++。在我看来,这对他们来说是一个糟糕的决定……但我不在那里工作,所以没有发言权:)
因此,不幸的是,您需要将库包装在一个薄的 obj-c 包装器中……或者(恕我直言)做明智的事情,避免像瘟疫一样迅速。它只是 objective-c 上的一个复杂的语法糖衣,不幸的是,如果不完全理解 objective-c,您将无法在 swift 中编写正式的应用程序。
</rant>
:)
关于c++ - Swift 使用静态库编译源作为 Objective-C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35873594/