如何读取 zig 中的文件并逐行运行?
我确实发现了os.File.openRead,但它似乎很旧,因为它这么说 container 'std.os' has no member called 'File'。
在阅读 zig 的文档时,我的印象是 zig 可以编译 C 和 C++ 代码。因此,我认为您可以通过导入 C++ 文件的头@cImport并zig build成功。但是,我似乎无法让它适用于 C++ 库集成。
我首先创建我的项目,zig init-lib然后src/main.zig通过@cImport指令添加我的导入。具体来说,我@cInclude("hooks/hooks.h")是这个库的C++头文件。如果我zig build此时尝试这样做,构建会失败,无法找到标头。我通过修改为build.zig来解决这个问题lib.addIncludeDir("/usr/include/library")。
由于这个 C++ 库现在正在被解析并使用 C++ 标准库,因此我收到的下一个错误zig build是stdexcept找不到标头。为了解决这个问题,我修改build.zig为lib.linkSystemLibrary("c++").
最后,我现在遇到的错误是/path/to/zig-linux-x86_64-0.9.1/lib/libcxx/include/<files>. 我得到诸如unknown type name '__LIBCPP_PUSH_MACROS, unknown type name 'namespace', 或 之类的东西unknown type name 'template'。谷歌搜索,我能找到的唯一部分相关的事情是,这是由于 clang 对 .h 文件的默认解释是 C 文件,显然没有namespace或template关键字,但我不知道该怎么办知识。 …
只是探索 Zig...我有一个 .zig 文件,其中包含一堆 comptime 函数和常量,我想在其他 .zig 程序中使用它们。相当于#include "my.h"C中的。
我现在正在学习zig语言。我见过const类似关键字的结构体的定义
const X = struct {
n: i32,
};
Run Code Online (Sandbox Code Playgroud)
我的理解是,const是一种互补var,后者允许变化,前者不允许。但是用 定义 struct 意味着什么var?
var Y = struct {
n: i32,
};
Run Code Online (Sandbox Code Playgroud)
这是合法的吗?我编译,所以是的。但这有什么意义和用途呢?
我正在使用 Zig 0.7.0.,并尝试从数组导入 Zig 源文件列表。每个源文件都有一个我想调用的main函数(其返回类型为)。!void该数组module_names在编译时是已知的。
这是我尝试做的:
const std = @import("std");
const log = std.log;
const module_names = [_][]const u8{
"01.zig", "02.zig", "03.zig", "04.zig", "05.zig",
};
pub fn main() void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
for (module_names) |module_name, i| {
const module = @import(module_name); // this fails
log.info("i {}", .{i});
try module.main();
}
}
Run Code Online (Sandbox Code Playgroud)
即使数组在编译时已知,@import(module_name)也会出现以下错误:
./src/main.zig:13:32: error: unable to evaluate constant expression
const module = @import(module_name);
^
./src/main.zig:13:24: note: referenced …Run Code Online (Sandbox Code Playgroud) 在 Zig(当前使用 0.7.1)中,假设由于某种原因您没有任何好的方法来设计resume对于每个suspend. 是否有任何支持的方法可以在运行时检测给定的帧是否已执行完成?
// overly simplistic example designed to illustrate the problem
pub fn main() void {
var frame = async amain(2);
resume frame;
resume frame;
// panic/UB -- resume async function which already returned
//
// we would prefer to have some kind of check which could allow
// us to detect at runtime that the following resumption is illegal
resume frame;
}
fn amain(ubound: i32) void {
var i: i32 = 0;
while …Run Code Online (Sandbox Code Playgroud) 尝试使用需要字符串的 Zig 库...但我从 C 库获得了字符串缓冲区。
这意味着我需要将类型值传递[*c]u8给接受[:0]const u8.
怎么做?
到目前为止我发现了这种方法:
const buffer: [*c]u8 = callC();
const str = std.mem.span(@ptrCast([*:0]const u8, buffer));
Run Code Online (Sandbox Code Playgroud)
这看起来比应有的更复杂(并且制作了副本??)。
Zig 文档说:
字符串文字是指向 u8 的以 null 结尾的数组的 const 指针。
所以我认为它们是兼容的 C 字符串,并且像这样的非常简单的转换@as([*:0]const u8, buffer)就足够了?
我试图用通用算法来实现向量代数,最后却选择了迭代器。我发现了两个不明显和意外行为的例子:
p指向带有 field 的结构(实例)的指针fi,我可以像p.fi(而不是p.*.fi)一样简单地访问该字段fun(this: *Self)(其中Self = @This())和结构体的实例s,我可以像s.fun()(而不是(&s).fun())一样简单地调用该函数我的问题是:
如何[]const []const u8在不使用分配器的情况下构建?
我可以
var slice: []const []const u8 = undefined;
slice.len = 0;
// use slice
Run Code Online (Sandbox Code Playgroud)
但肯定有更好的方法。
我发现 Zig 函数参数是恒定的。这意味着我释放 a 的幼稚功能HashMap不起作用。您可以在此处查看代码示例。我想知道最正确的 Zig 方式是否是dict作为函数传递,或者是否有其他方式可以使参数可变。
const Dict = std.StringHashMap;
fn releaseDict(allocator: Allocator, dict: Dict(i16)) void {
var iter = dict.iterator();
while (iter.next()) |entry|
allocator.free(entry.key_ptr.*);
dict.deinit();
}
Run Code Online (Sandbox Code Playgroud)