clang (objective-C) 模块和 Swift 模块有什么区别?

What is the difference between a clang (objective-C) module and a Swift module?

Clang 模块已记录在案 here, and Swift modules are here,但没有详细介绍。

由于 clang 是 LLVM 前端编译器,Swift 也在内部使用它,所以 Swift 模块是否总是 Clang 模块?它们是一回事吗?

它们是不同的。 不过在构建过程结束时,它们都需要链接到您的应用程序/库的其他 .o.dylib 文件到 运行.

Swift 个模块

  • From Swift Serialization.md docs:

    The fundamental unit of distribution for Swift code is a module. A module contains declarations as an interface for clients to write code against.

  • Swift acccess control docs:

    A module is a single unit of code distribution: a framework or application that’s built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.

  • .target()Package.swift

    中配置
  • 不能有子模块,因此用户不能在 Swift 中 import Module.Submodule。用户仍然可以导入特定实体,import struct PackageModel.Manifest,但这比导入子模块要冗长得多。

  • 它的接口作为.swiftmodule存在。 What is a .swiftmodule?. The documentation 说:

    Conceptually, the file containing the interface for a module serves much the same purpose as the collection of C header files for a particular library.

  • The compiler produces this .swiftmodule file a lot, like a generated objective-C header, but instead of text, its a binary repesentation. It includes the bodies of inlinable functions, much like static inline functions in objective-C or header implementations in C++. However, Swift modules does include the names and types of private declarations. This allows you to refer to them in the debugger, but it does mean you shouldn't name a private variable after your deepest darkest secret. from WWDC 2018: Behind the Scenes of the Xcode Build Process

    • 因此私有声明在您的 .swiftmodule(Swift 模块接口)中公开。
  • 将纯 Objective-C 框架导入 Swift 时,Swift 编译器使用其 built-in clang 编译器导入 Objective-C header.

The importer finds declarations in the headers exposed in Clangs .modulemap for that framework. (again, from WWDC2018)

  • 将 Objective-C + Swift 框架导入 Swift 时,Swift 编译器使用 Umbrella header.

Clang 模块

  • YourModuleName.modulemap 文件配置(以前是 module.map,但已弃用),格式如 this
  • 可以有子模块,例如std 模块有 std.iostd.complex.
  • clang 模块公开模块映射中指定的 header 文件。私人细节(在 .m 中)根本不会暴露。
  • 是对原始#include#import样式导入的改进,以改进构建过程(这是一个很大的话题,请阅读Clang module docs)。