Struct Zig 0.7.0中C函数的值返回的Access结构

Struct Zig 0.7.0中C函数的值返回的Access结构,struct,zig,Struct,Zig,下面是我在Zig 0.7.0中通过linux-x86_64上的@cImport导入Zig的一些C代码。当我在Zig中直接创建一个struct Pointstruct时,它会按预期工作,但当我从getPoint方法返回一个by value时,数据不正确(请参见下面的“输出”)。我做错了什么,还是这是一个错误 h点 c点 输出 build.zig 这可能有助于添加Zig版本和环境,您的示例在使用gnu abi的0.6.0 Windows building上的效果与我预期的一样。感谢您

下面是我在Zig 0.7.0中通过linux-x86_64上的@cImport导入Zig的一些C代码。当我在Zig中直接创建一个
struct Point
struct时,它会按预期工作,但当我从
getPoint
方法返回一个by value时,数据不正确(请参见下面的“输出”)。我做错了什么,还是这是一个错误

  • h点
  • c点
  • 输出
  • build.zig

这可能有助于添加Zig版本和环境,您的示例在使用gnu abi的0.6.0 Windows building上的效果与我预期的一样。感谢您的建议-我正在x86_64 linux上使用0.7.0,这已添加到帖子中。出于好奇,我在我的Windows机器上安装了Zig,并在针对Windows gnu的0.7.0上运行了它,它在我这方面也按预期工作。我想我会在我的Linux设置上做一点更健全的检查,然后发布一个github问题。这很有趣,我的第一个想法是一些与打包相关的东西,但是我当前的机器没有为msvc设置,所以我不能轻松地戳一下。也许可以尝试使用结构成员类型,祝你好运!这可能有助于添加Zig版本和环境,您的示例在使用gnu abi的0.6.0 Windows building上的效果与我预期的一样。感谢您的建议-我正在x86_64 linux上使用0.7.0,这已添加到帖子中。出于好奇,我在我的Windows机器上安装了Zig,并在针对Windows gnu的0.7.0上运行了它,它在我这方面也按预期工作。我想我会在我的Linux设置上做一点更健全的检查,然后发布一个github问题。这很有趣,我的第一个想法是一些与打包相关的东西,但是我当前的机器没有为msvc设置,所以我不能轻松地戳一下。也许可以尝试使用结构成员类型,祝你好运!

    struct Point {
        int x;
        int y;
        int z;
    };
    
    struct Point getPoint(void);

    #include "point.h"
    #include <stdio.h>
    
    struct Point getPoint() {
        struct Point retVal = { .x=50, .y=50, .z=50 };
        return retVal;
    }


    const std = @import("std");
    const c = @cImport({
        @cInclude("point.h");
    });
    
    pub fn main() void {
        var point = c.getPoint();
        var anotherPoint = c.Point{ .x = 50, .y = 50, .z = 50 };
        std.debug.print("point x: {} y: {} z: {}\n", .{ point.x, point.y, point.z });
        std.debug.print("anotherPoint x: {} y: {} z: {}\n", .{ anotherPoint.x, anotherPoint.y, anotherPoint.z });
    }


    point x: 50 y: 50 z: -1705967616
    anotherPoint x: 50 y: 50 z: 50

    const Builder = @import("std").build.Builder;
    
    pub fn build(b: *Builder) void {
        // Standard target options allows the person running `zig build` to choose
        // what target to build for. Here we do not override the defaults, which
        // means any target is allowed, and the default is native. Other options
        // for restricting supported target set are available.
        const target = b.standardTargetOptions(.{});
        //const lib = b.addStaticLibrary("interface", "src/libinterface.a");
    
        // Standard release options allow the person running `zig build` to select
        // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
        const mode = b.standardReleaseOptions();
    
        const exe = b.addExecutable("point_test", "src/main.zig");
        exe.setTarget(target);
        exe.setBuildMode(mode);
        exe.linkLibC();
        exe.addIncludeDir("src");
        exe.install();
        exe.addCSourceFile("src/point.c", &[_][]const u8{
            "-Wall",
            "-Wextra",
            "-Werror",
        });
    
        const run_cmd = exe.run();
        run_cmd.step.dependOn(b.getInstallStep());
        if (b.args) |args| {
            run_cmd.addArgs(args);
        }
    
        const run_step = b.step("run", "Run the app");
        run_step.dependOn(&run_cmd.step);
    }