简体   繁体   中英

Access Structs Returned by Value from C Functions in Zig 0.7.0

Here's some C code that I import into Zig via @cImport on linux-x86_64, in Zig 0.7.0. When I directly create a struct Point struct in Zig it works as expected, but when I return one by value from the getPoint method have bad data (see "output" below). Am I doing something wrong, or is this a bug?

  • point.h

    struct Point {
        int x;
        int y;
        int z;
    };
    
    struct Point getPoint(void);
  • point.c

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

  • main.zig

    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 });
    }

  • output

    point x: 50 y: 50 z: -1705967616
    anotherPoint x: 50 y: 50 z: 50
  • build.zig

    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);
    }

Zig's c abi compatability currently has some issues with structs and floats.

The specific issue you are experiencing, #3211 , has been fixed and your code will now work.

$> zig run main.zig point.c -I.
point x: 50 y: 50 z: 50
anotherPoint x: 50 y: 50 z: 50

However, issues still remain with C abi interop eg: #9487

Until all of these issues are fixed, it can often be worked around by using pointers rather than pass-by-value for arguments and return values

// workaround.h

#include "point.h"

void workaround_getPoint(struct Point* out);
// workaround.c

#include "workaround.h"

void workaround_getPoint(struct Point* out) {
    *out = getPoint();
}
// .zig
const c = @cImport({
    @cInclude("point.h");
    @cInclude("workaround.h");
});
pub fn getPoint(): c.Point {
    var res: c.Point = undefined;
    c.workaround_getPoint(&res);
    return res;
}
// build.zig
        exe.addCSourceFile("src/workaround.c", &.{ "-Wall", "-Wextra", "-Werror" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM