Skip to content

cImport namespacing is too strict for C structs across multiple zig files #12073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pluick opened this issue Jul 11, 2022 · 2 comments
Closed
Labels
translate-c C to Zig source translation feature (@cImport)

Comments

@pluick
Copy link
Contributor

pluick commented Jul 11, 2022

Zig Version

0.10.0-dev.7729+b88151e0e

Steps to Reproduce

File struct.h

typedef struct astruct {
        int id;
} astruct;
void printa(astruct);

File struct.c

include "struct.h"
void printa(astruct a) { }

File main.zig

const c = @cImport({ @cInclude("struct.h"); });
const other = @import("other.zig");

pub const astruct = c.astruct;

pub fn main() anyerror!void {
    var a = astruct {.id = 4};
    other.pa(a);
}

File other.zig

const std = @import("std");
const main = @import("main.zig");
const c = @cImport({ @cInclude("struct.h"); });

// This would fail
//comptime { std.debug.assert( main.astruct == c.astruct ); }

pub fn pa(a: main.astruct) void {
    c.printa(a);
}

And then attempt to compile the code.

Expected Behavior

Successful compilation - this kind of aliasing can be done with Zig structs, so I would expect the same from c structs.

Actual Behavior

Compilation fails with this error message:

./src/other.zig:9:14: error: expected type '.cimport:3:11.struct_astruct', found '.cimport:1:11.struct_astruct'
    c.printa(a);
             ^
./zig-cache/o/05ef4e7a090cb6b35ba9a3f8b92c62a6/cimport.zig:55:28: note: .cimport:3:11.struct_astruct declared here
pub const struct_astruct = extern struct {
                           ^
./zig-cache/o/05ef4e7a090cb6b35ba9a3f8b92c62a6/cimport.zig:55:28: note: .cimport:1:11.struct_astruct declared here
pub const struct_astruct = extern struct {

Which appears to be referencing different declarations of the same c struct (.cimport:1 vs .cimport:3)

@pluick pluick added the bug Observed behavior contradicts documented or intended behavior label Jul 11, 2022
@pluick
Copy link
Contributor Author

pluick commented Jul 11, 2022

Some extra info:

  • if the aliasing is done within the same zig file, it will not fail
  • this also fails if c.astruct is wrapped inside a zig struct, and then passed to a separate zig file
  • My use case that discovered this is writing a binding layer to c code

@Vexu
Copy link
Member

Vexu commented Jul 11, 2022

Each @cImport causes the file to be translated again creating new, distinct types. This is why it is advisable to only have one @cImport in your project. See the documentation here https://ziglang.org/documentation/master/#cImport.

Related #4017

@Vexu Vexu closed this as completed Jul 11, 2022
@Vexu Vexu added translate-c C to Zig source translation feature (@cImport) and removed bug Observed behavior contradicts documented or intended behavior labels Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

2 participants