标签: zig

使用 Zig 编译器构建 C 代码时如何包含 (msvc) libc

我最近发现了 zig 并发现它非常有趣。我现在正在尝试学习如何使用 zig 作为交叉编译器,并且以下构建和运行良好(在 Windows 上)

zig cc -Wno-everything src/ctest.c
Run Code Online (Sandbox Code Playgroud)

但是,当我使用 build-exe 命令或构建脚本时,无法找到并链接 (Windows) libc

c:\zigctest>zig build

Zig is unable to provide a libc for the chosen target 'x86_64-unknown-windows-msvc'.
The target is non-native, so Zig also cannot use the native libc installation.
Choose a target which has a libc available, or provide a libc installation text file.
See `zig libc --help` for more details.
The following command exited with error code 1:
c:\zigctest\zig.exe build-exe --library c --c-source -Wno-everything C:\zigctest\src\ctest.c …
Run Code Online (Sandbox Code Playgroud)

windows build libc zig

5
推荐指数
1
解决办法
3294
查看次数

Malloc 到 Zig 中的结构列表?

如何动态分配内存空间并获取指向 Zig 中结构列表的指针。

就像在 C 中一样:

struct Foo* my_array_of_foo = (struct Foo*) malloc(10*sizeof(Foo));
Run Code Online (Sandbox Code Playgroud)

zig

5
推荐指数
1
解决办法
4056
查看次数

在 Zig lang 中分配通用结构

是否可以为struct包含类型作为属性的对象创建分配?

例如

示例结构是

const Content = struct {
    content: type,
    name: []const u8,
};
Run Code Online (Sandbox Code Playgroud)

然后,我想分配内存,例如2*Content

我知道我可以使用

const list: [2]CustomElement = .{ Content{ .content = Type, .name = "test" }, Content{ .content = Type, .name = "test" } };
Run Code Online (Sandbox Code Playgroud)

但如何使用它们来实现同样的事情allocators呢?

这是行不通的。

  comptime {
        var list = std.ArrayList(Content).init(test_allocator);
        try list.append(Content{ .content = MyType , .name = "test" });
    }
Run Code Online (Sandbox Code Playgroud)

我收到错误

error: parameter of type '*std.array_list.ArrayListAligned(Content,null)' must be declared comptime
Run Code Online (Sandbox Code Playgroud)

简而言之

Box是否可以像Rust 中那样构建功能?

allocator zig

5
推荐指数
1
解决办法
2460
查看次数

在 zig 中搜索结构体的 ArrayList

我希望这个问题有一个非常简单的答案,关于如何在 zig 中做好这件事。

我想搜索某个结构的 ArrayList 以通过其中一个字段查找记录。
在 C++ 中,我会考虑使用 std::find_if 和 lambda,但 zig 标准库中似乎没有类似的东西,除非我错过了一些东西。

有没有比下面这样的简单循环更好/更惯用的方法?

const std = @import("std");

const Person = struct {
    id: i32,
    name: []const u8
};

pub fn main() !void {

    const allocator = std.heap.page_allocator;

    var data = std.ArrayList(Person).init(allocator);
    defer data.deinit();

    try data.append(.{.id = 1, .name = "John"});
    try data.append(.{.id = 2, .name = "Dave"});
    try data.append(.{.id = 8, .name = "Bob"});
    try data.append(.{.id = 5, .name = "Steve"});

    // Find the id of the person …
Run Code Online (Sandbox Code Playgroud)

zig

5
推荐指数
1
解决办法
1787
查看次数

在 Zig 中循环整数范围

像这样的 while 循环是在 Zig 中循环整数范围的惯用方法吗?

var i: i32 = 5;
while (i<10): (i+=1) {
    std.debug.print("{}\n", .{i});
}
Run Code Online (Sandbox Code Playgroud)

我首先尝试了类似Python的

for (5..10) |i| {
    // ....
Run Code Online (Sandbox Code Playgroud)

但这行不通。

zig

5
推荐指数
2
解决办法
3426
查看次数

使用 Zig 中的 comptime 值格式化字符串

我正在尝试创建一个仅接受长度为 4 的内置向量(如@Vector)的多态函数。据我了解,向量的长度是编译时已知的,我想在编译时错误消息中添加更多信息。我知道它@typeName可以用于将类型转换为 comptime 字符串,但是可以用什么来转换 comptime 值呢?

/// x[3] == 1
/// x must be a built-in vector of length 4
pub fn ispoint(x: anytype) bool {
    const T = @TypeOf(v);
    switch (@typeInfo(T)) {
        .Vector => |info| {
            if (info.len != 4) {
                // TODO: report length of provided argument
                @compileError("Not a valid tuple, found Vector of length ???");
            }
            return v[3] == 1;
        },
        else => @compileError("`ispoint` expected a `@Vector(4, T)`, found " ++ @typeName(T)), …
Run Code Online (Sandbox Code Playgroud)

string formatting metaprogramming zig

5
推荐指数
1
解决办法
971
查看次数

如何从 Zig 中的字符串解析整数?

在 Zig 中从字符串解析整数并指定结果整数类型的最佳方法是什么?

const foo = "22";
Run Code Online (Sandbox Code Playgroud)

例如,我如何转换foo为?i32

string integer zig

5
推荐指数
1
解决办法
4252
查看次数

如何在 zig 的结构字段中执行泛型

我对 zig 很陌生,我想知道如何创建一个可以具有编译时已知类型的结构字段

例如,comptime与函数参数一起使用时类似于关键字的东西,我想对作为数组的结构体字段执行相同的操作

in 函数的示例comptime

fn exampleFn(comptime t: type, allocator: std.mem.Allocator) ![]t {
    var array: []t = try allocator.alloc(t, 3);
    return array;
}
Run Code Online (Sandbox Code Playgroud)

在这里我可以指定任何类型作为输出数组的类型(这是我能想到的最好的例子)

我想要做的事情的例子:

const List = struct {
    content_type: type,
    data: ?[].content_type,
};
Run Code Online (Sandbox Code Playgroud)

有什么方法可以做这样的事情并且仍然能够在运行时使用该结构?

generics struct zig

5
推荐指数
1
解决办法
1076
查看次数

使用基于 LLVM 的工具(clang、Rust、Zig)为 N64 进行编译

I\xe2\x80\x99m 尝试使用基于 LLVM 的zig cc为 R4300i / VR4300 MIPS CPU(N64 中的处理器)编译 C 代码(最终还有其他基于 LLVM 的语言,如 Rust/Zig)。但我没有看到任何以 VR4300 作为目标进行编译的证据或示例:

\n
    \n
  • 我见过的编译指南/工具\xe2\x80\x99都指导使用GCC / IDO进行编译,没有提到基于LLVM的C编译器(甚至没有提到像clang这样的更主流的编译器)。
  • \n
  • 我没有看到它列在 中zig targets
  • \n
  • 我没有看到任何方法来列出可用于 LLVM 的有效目标。
  • \n
\n

LLVM不支持VR4300作为编译目标吗?我怎样才能自己发现这一点?

\n

mips llvm clang rust zig

5
推荐指数
0
解决办法
338
查看次数

检查 zig 数组中是否存在元素

有没有更惯用的方法来查找数组中是否包含某个值

fn valueInArray(value: u32, array: []const u32) bool {
    for (array) |num| {
        if (value == num) {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

zig

5
推荐指数
1
解决办法
499
查看次数

标签 统计

zig ×10

string ×2

allocator ×1

build ×1

clang ×1

formatting ×1

generics ×1

integer ×1

libc ×1

llvm ×1

metaprogramming ×1

mips ×1

rust ×1

struct ×1

windows ×1