Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Dec 3, 2023
1 parent 329ce3c commit 35c7ef9
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions learn/advanced/package_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ zig 当前并没有一个中心化存储库,包可以来自任何来源,无

- `name`:当前你所开发的包的名字
- `version`:包的版本,使用 [Semantic Version](https://semver.org/)
- `dependencies`:依赖项,内部是一连串的匿名结构体,字段 `dep_name` 是依赖包的名字,`url` 是源代码地址,`hash` 是对应的hash(源文件内容的hash)
- `dependencies`:依赖项,内部是一连串的匿名结构体,字段 `dep_name` 是依赖包的名字,`url` 是源代码地址,`hash` 是对应的 hash(源文件内容的 hash)。
- `paths`:显式声明包含的源文件,包含所有则指定为空,当前仅 `nightly` 可用。

目前为止,`0.11` 版本支持两种打包格式的源文件:`tar.gz``tar.xz`
Expand Down Expand Up @@ -76,15 +76,15 @@ pub fn build(b: *std.Build) void {

:::

## 引入依赖项
## 引入包

可以使用 `build` 函数传入的参数 `b: *std.Build`,它包含一个方法 [`dependency`](https://ziglang.org/documentation/master/std/#A;std:Build.dependency), 它的原型如下:

```zig
fn dependency(b: *Build, name: []const u8, args: anytype) *Dependency
```

其中 `name` 是在在 `.zon` 中的依赖项名字,它返回一个 [`*std.Build.Dependency`](https://ziglang.org/documentation/master/std/#A;std:Build.Dependency),可以使用 `artifact``module` 方法来访问依赖的链接库和暴露的 `module`
其中 `name` 是在在 `.zon` 中的包名字,它返回一个 [`*std.Build.Dependency`](https://ziglang.org/documentation/master/std/#A;std:Build.Dependency),可以使用 `artifact``module` 方法来访问包的链接库和暴露的 `module`

```zig
const std = @import("std");
Expand All @@ -98,14 +98,14 @@ pub fn build(b: *std.Build) void {
// ...
// 获取依赖项
// 获取包
const package = b.dependency("package_name", .{});
// 获取依赖项构建的library,例如链接库
// 获取包构建的library,例如链接库
const library_name = package.artifact("library_name");
// 获取依赖项提供的模块
// 获取包提供的模块
const module_name = package.module("module_name");
// ...
Expand All @@ -123,16 +123,16 @@ pub fn build(b: *std.Build) void {
```

如果需要引入一个本地依赖项(且该依赖项自己有 `build.zig`),那么可以使用 [`std.Build.anonymousDependency`](https://ziglang.org/documentation/master/std/#A;std:Build.anonymousDependency), 它的原型为:
如果需要引入一个本地包(且该包自己有 `build.zig`),那么可以使用 [`std.Build.anonymousDependency`](https://ziglang.org/documentation/master/std/#A;std:Build.anonymousDependency), 它的原型为:

```zig
fn anonymousDependency(b: *Build, relative_build_root: []const u8, comptime build_zig: type, args: anytype) *Dependency
```

参数为依赖项的包构建根目录和通过 `@import` 导入的依赖项的 `build.zig`
参数为包的包构建根目录和通过 `@import` 导入的包的 `build.zig`

::: info 🅿️ 提示

`dependency``anonymousDependency` 都包含一个额外的参数 `args`这是传给对应的依赖项构建的参数(类似在命令行构建时使用的 `-D` 参数,通过 [`std.Build.option`](https://ziglang.org/documentation/master/std/#A;std:Build.option) 实现),当前包的参数并不会向依赖项传递,需要手动显式指定转发。
`dependency``anonymousDependency` 都包含一个额外的参数 `args`这是传给对应的包构建的参数(类似在命令行构建时使用的 `-D` 参数,通过 [`std.Build.option`](https://ziglang.org/documentation/master/std/#A;std:Build.option) 实现),当前包的参数并不会向包传递,需要手动显式指定转发。

:::

0 comments on commit 35c7ef9

Please sign in to comment.