Itâs generally not recommended to include the implementation of a library in a @cImport. @cImport, which internally translates the C code to Zig code, is not working in many cases yet.
See Using a single-header C library from Zig - #2 by IntegratedQuantum for an explanation on how to deal with header-only libraries in zig.
Still getting the same error, just moved to the C file:
zig build-exe init-exe Debug native: error: error(compilation): clang failed with stderr: In file included from (root)\src\enet_impl.c:2:
(root)\libs/enet.h:4978:23: error: use of undeclared identifier 'CLOCK_MONOTONIC'
the error seems to hint at a problem that you havenât solved yet: it expects a CLOCK_MONOTONIC to be defined, while itâs not. you probably are missing an expected import.
Are you linking libc when building Zig? When building against the C standard library you need to add the -lc flag to calls to zig build-exe or add something along the lines of exe.linkLibC() to your build.zig.
The CLOCK_MONOTONIC definition should be included from time.h so I expect that the undeclared identifier comes from cInclude not knowing how to find time.h.
OP is indeed linking to libc, you can see that line in the build script. That said, if that symbol is defined in time.h, then it might simply be a matter of cInclude-ing it before enet.h.
Oh, of course. Hmm, but as it it is included inside enet.h shouldnât an extra include not be needed? The library seems to build and work fine for me with just a single cInclude("enet.h"). I am on x86_64-linux though.
the error seems to hint at a problem that you havenât solved yet: it expects a CLOCK_MONOTONIC to be defined, while itâs not. you probably are missing an expected import.
Certainly, but that doesnât help if I canât figure out what import that is. I should have mentioned in the OP Iâve tried stdio.h, time.h, and windows.h .None have helped
The library seems to build and work fine for me
Thatâs interesting to note. Donât have a linux enviornment to test on myself
Maybe you need a #define to enable that symbol? For example:
Good to see that you found a solution. A lot of C/C++ projects have these kinds of quirks to them. Tiny inconsistencies between different platforms, things getting occasionally out of sync, etc.
The only solution is just to learn how to deal with them systematically⌠or to wait for somebody else to create a build.zig file and package the library for you.
Welcome to the bleeding edge of systems programming!
Although this is not exactly a flaw with the language, but a missing piece in the toolchain. Without Zig you would have had to procure the entirety of the required headers and libs on your own, and in this case you got most of them, just not all.
If you look at the thread on GH thereâs a PR that aims to solve this problem, so a kind contributor has already done some work to solve your problem, it just hasnât been merged yet.
So my suggestion is to subscribe to that PR and wait for it to be merged.
As an optional step, you could also check out that branch, build Zig from it, and use the PR even without it being merged, the problem in this case is that it seems to have incurred in some regressions (CI is red on most hosts), so you probably wonât get a working source tree right away, but it might also be that those are easy fixes, so it might prove a viable path afterall.