我有一个包含 C 静态库的 Swift 框架。该框架用于 iOS 应用程序。
如果我对我的框架执行 carthage archive
,然后将输出带到我的应用程序中的一个单独的 Xcode 项目中,然后当它尝试编译应用程序时,它会在 CompileSwift
期间停止运行命令:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "my_sdk.h"
^
/Users/myUser/my-swift-sdk/ext/my-c-sdk/include/c_sdk.h:10:10: note: in file included from /Users/myUser/my-swift-sdk/ext/my-c-sdk/include/c_sdk.h:10:
#include "alpha/alpha.h"
^
/Users/myUser/my-swift-sdk/ext/my-c-sdk/include/alpha/alpha.h:12:10: note: in file included from /Users/myUser/my-swift-sdk/ext/my-c-sdk/include/alpha/alpha.h:12:
#include "../bravo/bravo.h"
^
/Users/myUser/my-swift-sdk/ext/my-c-sdk/include/alpha/../bravo/bravo.h:13:10: error: 'omega.h' file not found
#include "omega.h"
^
<unknown>:0: error: could not build Objective-C module 'libMyCSdk'
0 swift 0x000000010680664a PrintStackTraceSignalHandler(void*) + 42
1 swift 0x0000000106805dfe SignalHandler(int) + 302
2 libsystem_platform.dylib 0x00007fff60ee7b3d _sigtramp + 29
3 libsystem_platform.dylib 0x0000000100020812 _sigtramp + 2668858610
4 swift 0x0000000103f7a197 swift::GenericSignatureBuilder::addRequirement(swift::Requirement const&, swift::GenericSignatureBuilder::FloatingRequirementSource, swift::ModuleDecl*) + 887
5 swift 0x0000000104005922 substType(swift::Type, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<llvm::Optional<swift::ProtocolConformanceRef> (swift::CanType, swift::Type, swift::ProtocolType*)>, swift::SubstOptions) + 2882
6 swift 0x0000000104011318 swift::TypeBase::getTypeOfMember(swift::ModuleDecl*, swift::ValueDecl const*, swift::Type) + 168
7 swift 0x0000000103b01582 swift::CalleeCandidateInfo::CalleeCandidateInfo(swift::Type, llvm::ArrayRef<swift::constraints::OverloadChoice>, bool, swift::constraints::ConstraintSystem&, bool) + 1122
8 swift 0x0000000103a90b2c (anonymous namespace)::FailureDiagnosis::visitApplyExpr(swift::ApplyExpr*) + 10828
9 swift 0x0000000103a6e206 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 70
10 swift 0x0000000103aa15af swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6207
11 swift 0x0000000103ba644a swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 122
12 swift 0x0000000103ba6b27 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 1207
13 swift 0x0000000103a7ee5c (anonymous namespace)::FailureDiagnosis::typeCheckChildIndependently(swift::Expr*, swift::Type, swift::ContextualTypePurpose, swift::OptionSet<TCCFlags, unsigned int>, swift::ExprTypeCheckListener*, bool) + 1516
14 swift 0x0000000103a76a40 swift::ASTVisitor<(anonymous namespace)::FailureDiagnosis, bool, void, void, void, void, void>::visit(swift::Expr*) + 25280
15 swift 0x0000000103a6e206 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 70
16 swift 0x0000000103aa15af swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 6207
17 swift 0x0000000103ba644a swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 122
18 swift 0x0000000103ba6b27 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 1207
19 swift 0x0000000103c3e9ac swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 12044
20 swift 0x0000000103c3abbe swift::TypeChecker::typeCheckAbstractFunctionBodyUntil(swift::AbstractFunctionDecl*, swift::SourceLoc) + 1294
21 swift 0x0000000103c40468 swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 888
22 swift 0x0000000103c67013 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 2515
23 swift 0x00000001038a1865 swift::CompilerInstance::performSema() + 4949
24 swift 0x0000000102a8e59b performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 731
25 swift 0x0000000102a8adc5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
26 swift 0x0000000102a30a35 main + 1349
27 libdyld.dylib 0x00007fff60cfe085 start + 1
Stack dump:
0. Program arguments: [The stuff sent to SwiftCompile - lots to redact so will add if required]
1. While type-checking 'addDevice()' at /Users/myUser/my-app/Sources/Views/MyView1/MyView1ViewController.swift:54:13
2. While type-checking statement at [/Users/myUser/my-app/Sources/Views/MyView1/MyView1ViewController.swift:54:30 - line:94:5]
[code and some more 'While type-checking expression']
error: Segmentation fault: 11
我不得不编辑它/更改路径,因为它不是一个开源项目,但所有重要的东西都应该在那里。
如果我在 Xcode 中编译框架然后将生成的二进制文件复制到应用程序中的 Carthage 目录,则不会发生这种情况;该应用程序构建良好。框架大小不同,但这是预期的,因为 Carthage 将为 sim 和设备创建一个胖二进制文件,而由 Xcode 生成的框架只会做其中之一。
我尝试让 Carthage 在调试配置中构建和归档;这也不起作用(调试将包括进行任何优化/剥离)。
编辑:使用该框架的 iOS 应用仅在为模拟器构建时中断。如果我为设备构建,那很好。
编辑:我试过建议here关于从命令行使用 xcodebuild 时添加目标,即 xcodebuild clean build -scheme App -configuration Debug -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 8'
- 没有用。
编辑:为了明确说明,我也对 DerivedData 进行了多次核对 :)
编辑:环境是 Xcode 10.0,在 Mojave 上使用 iOS 12 SDK 构建
编辑:更改标题以反射(reflect)新的研究。迦太基是一条红鲱鱼。问题是这样的:
- 如果 DerivedData 中的模块缓存包含 C 静态库的条目(例如,当它作为构建 Swift 框架的一部分构建时),应用程序将构建。
- 如果在构建 Swift 框架和构建应用程序之间清除 DerivedData,则会发生上述错误情况。
- Carthage 表现出第二种行为,因为它使用单独的 DerivedData 目录构建,具有自己的模块缓存,这意味着当尝试构建应用程序时,应用程序正在使用的缓存中没有 C 库的模块(默认一个)。
- 对于大多数带有 C 或 Objective-C 模块的链接框架,在构建应用程序时,它们会在模块缓存中获得一个缓存条目。使用链接框架构建应用程序时,我的 C 静态库没有。它在构建框架本身时确实获得了一个条目。它可能得不到一个,因为 xcodebuild 无法执行缓存模块所需的任何操作,因为它遇到了上面的“缺失” header 。
编辑:有些成功!如果我将“ header 搜索路径”指向我的 C 静态库的包含目录,并在那里重命名 module.modulemap 文件,它都会编译。这不是解决方案,但确认 App 项目需要 header 以创建模块的缓存版本。
最佳答案
C 项目中的 header 是如何组织的?
如果他们有这种模式:
Lib/
Umbrella.h
A/
A1.h
A2.h
B/
B1.h
B2.h
然后您将需要执行特殊的 header 复制阶段,而不仅仅是正常的 header 复制阶段,因为在这种情况下,一旦复制到框架中,它们的相对路径就很重要。
关于ios - 未缓存模块时,iOS 框架中的 C 静态库“无法构建 Objective-C 模块”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52873512/