gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:40:35 30 4
gpt4 key购买 nike

Clang 模块已记录 here , Swift 模块是 here没有详细介绍。

既然 clang 是 Swift 内部也使用的 LLVM 前端编译器,那么 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.

  • Package.swift 中的 .target() 配置

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

  • 它的接口(interface)作为一个.swiftmodule存在。 What is a .swiftmodule? . 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

    • 因此私有(private)声明在您的 .swiftmodule(Swift 模块接口(interface))中公开。
  • 将纯 Objective-C 框架导入 Swift 时,Swift 编译器使用其内置的 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 模块公开模块映射中指定的头文件。私有(private)详细信息(在 .m 中)根本不会公开。
  • 是对原始 #include#import 样式导入的改进,以改进构建过程(这是一个很大的话题,请阅读 Clang module docs)。

关于swift - clang (objective-C) 模块和 Swift 模块有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68055447/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com