首页 > 解决方案 > 当模块未缓存时,“无法为 iOS 框架中的 C 静态库构建 Objective-C 模块”

问题描述

我有一个包含 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 在 Debug 配置中构建和存档;这也不起作用(调试将包括进行任何优化/剥离)。

编辑:使用该框架的 iOS 应用程序只会在为模拟器构建时中断。如果我为设备构建,那很好。

编辑:我在这里尝试了关于从命令行使用 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 构建

编辑:标题更改以反映新的研究。迦太基是一条红鲱鱼。问题是这样的:

编辑:一些成功!如果我将“Header Search Paths”指向我的 C 静态库的包含目录,并在那里重命名 module.modulemap 文件,它就会全部编译。这不是一个解决方案,但确认 App 项目需要标头以便创建模块的缓存版本。

标签: ioscswiftxcodecarthage

解决方案


C 项目中的标题是如何组织的?

如果他们有这种模式:

Lib/
    Umbrella.h
    A/
        A1.h
        A2.h
    B/
        B1.h
        B2.h

然后,您将需要执行特殊的标头复制阶段,而不仅仅是正常的标头复制阶段,因为在这种情况下,一旦复制到框架中,它们的相对路径就很重要。


推荐阅读