#Can't get cImport to work at all

1 messages · Page 1 of 1 (latest)

inner sluice
#

i have a really simple pair of files, main.zig and something.h w/ a single struct definition in the .h file and then trying to use that struct in .zig but the compiler keeps telling me "file not found" even though when i look in zig-out i can clearly see that it's compiling the .h file.

both files are in src/ and i don't know if that's a problem.
don't know if it's relevant, but i'm using windows -- for all i know it doesn't work on windows yet, but i didn't see anything in the docs saying so it's probably something i'm doing wrong

main.zig

const std = @import("std");
const something = @cImport({ @cInclude("src/something.h"); });

pub fn main() !void {
    var s: something.something = .{ .a = 1, .b = 2, .c = 3, };
    _ = s.a + s.b;
}

something.h

struct something {
    int a;
    int b;
    int c;
};

i have also added the following to build.zig

    exe.addIncludePath("src");
    exe.addCSourceFile("src/something.h", &[_][]const u8 {});
    exe.linkLibC();

so far i can only get it to give me 2 errors

  1. error: C import failed with no other details
  2. if i remove the src/ from the @cInclude statement, it tells me file not found
inner sluice
#

probably worth mentioning i'm using the master build of the compiler (installed today) after trying it on 0.10. results not different on either

hushed kraken
#

try removing the addCSourceFile call

#

(probably shouldn't be trying to compile a header file in the first place regardless of if that fixes it)

#

In theory the second solution you mentioned should work

noble loom
#

isn't addCSourceFile for .c files?

hushed kraken
#

ye

#

'course, some C compilers can emit Pre-Compiled Headers (PCHs), but I don't think that's a first class citizen of the build system

inner sluice
#

welp, when i pulled that line from google, it sounded sus because what if you need to include like a thousand of them lol

#

anyway that worked thank you gents

#

i really just thought... actuially i don't even know what i thought now that i'm looking at it again w/out that line. it seems so obvious that all i'd need is addIncludePath

hushed kraken
#

aye, sometimes all it takes is another brain or two

#

reasoning and logic is a social skill, in a weird way

inner sluice
#

it very much is

#

i'm using words to talk to a computer lol it must be