关闭-查看条目的结尾
我想使用zig语言创建一个C可调用的库。我决定从Zig文档中的两个示例开始。“导出C库”和“混合对象文件”。在每一种情况下,我都复制了三个相关文件(从0.6.0docs)。
这两个例子都无法建立。
导出C库示例无法编译带有消息的test.c,无法找到mathtest.h
混合对象Files示例无法编译test.c,无法找到find 64.h
下面是Exporting示例的三个文件:
mathtest.zig
export fn add(a: i32, b: i32) i32 {
return a + b;
}test.c
// This header is generated by zig from mathtest.zig
#include "mathtest.h"
#include <stdio.h>
int main(int argc, char **argv) {
int32_t result = add(42, 1337);
printf("%d\n", result);
return 0;
}build.zig
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
const exe = b.addExecutable("test", null);
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
exe.linkLibrary(lib);
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);
const run_cmd = exe.run();
const test_step = b.step("test", "Test the program");
test_step.dependOn(&run_cmd.step);
}错误消息的部分
~/Projects/zig/z-c-lib $ zig build test
/home/robert/Projects/zig/z-c-lib/test.c:2:10: fatal error: 'mathtest.h' file not found
#include "mathtest.h"
^~~~~~~~~~~~
1 error generated.
The following command failed:
/home/robert/zig/zig clang -c -nostdinc -fno-spell-checking -target x86_64-unknown-linux-gnu -isystem /home/robert/zig/lib/zig/include -isystem /home/robert/zig/lib/zig/libc/include/x86_64-linux-gnu -isystem /home/robert/zig/lib/zig/libc/include/generic-glibc -isystem /home/robert/zig/lib/zig/libc/include/x86_64-linux-any -isystem /home/robert/zig/lib/zig/libc/include/any-linux-any -Xclang -target-cpu -Xclang znver2 -Xclang -target-feature -Xclang -3dnow -Xclang -target-feature -Xclang -3dnowa -Xclang -target-feature -Xclang +64bit -Xclang -target-feature -Xclang +adx -Xclang -target-feature -Xclang +aes -Xclang -target-feature -Xclang +avx -Xclang -target-feature -Xclang +avx2 -Xclang -target-feature -Xclang -avx512bf16 -Xclang -target-feature -Xclang -avx512b我无法在我的系统中找到一个名为mathtest.h的文件,所以我假设它不是生成的,这与test.c文件中的声明相反。
我错过了什么?感激地接受帮助。
答案和更多问题
我发现了-femit-h选项:
zig build-lib mathtest.zig -femit-h将创建一个mathtest.h文件,然后
zig build一定会成功的。
我进一步发现build.zig文件中的这些行
const lib = b.addSharedLibrary('mathtest', 'mathtest.zig', b.version(1, 0, 0));
lib.femit_h = true;将确保
zig build test将成功地生成答案1379,如docs中所示。
但是- build.zig文件的这个模块在运行后不会留下一个mathtest.h文件。
这似乎是从Zig代码生成可用C库的最后一个障碍。
拼图中的最后一块
如果我加上
lib.setOutputDir("build");到build.zig文件。mathtest.h和libmathtest.a (或.so)文件将保存到build dir中。
称此为关闭
发布于 2020-10-20 19:00:01
好的,部分答案很简单,但可能是模糊的,女性-h选项。命令
zig build-lib mathtest.zig -femti-h将生成一个mathtest.h文件。但是,如何将该选项添加到
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));build.zig文件中的行。
https://stackoverflow.com/questions/64451405
复制相似问题