Pls explain to me what is the way to c import (guess is included)

Can you explain to me what is the way to import c libs in zig, with looking forward at zig lang changes?
I’ve seen that @cimport needs to be deleted and its in road map or something like that.
So, the most stable way will be to zig translate-c cimports.c ,import the resulting file, and then link libs through exe.linkSystemLibrary()? Or there is more convinient way to link the lib or to import it for now and the future. Im just a noob trying to learn practicaly, using some graphic libs, and dont want to use templates that dont tell me nothing about the build system.

1 Like

translate-c is accessible in the build system b.addTranslateC which you can get a module from with translate.createModule

For the time being, you can still use @cImport, which does translate-c in the background.

things to note:

don’t @cImport` a header multiple times, they will be considered different, which can cause problems such as type mismatches of supposedly the same types.

On zig master a new translate-c backend was recently merged, this has come with regressions and bugs.

Translate-c is primarily for header files, not source files.

You build c/c++ source files via modules addCFile[s] (modules don’t need to have any zig files), this can be with the one you get from the translate-c.

Often you put that module in a separate lib compilation, but you could just import it too, though that is less flexible.

you might need to link libc/cpp, it wont do it for you, but it depends on the libraries you are using.

if you intend to distribute your project you should support using linkSystemLibrary, as well as static builds. The former is prefered by system packagemanagers, the latter is preferd for building from source, or distributing already compiled artifacts. That is a longer term suggestion, you shouldn’t work on that immediately.

2 Likes

thank you for such techincal answer! i will certainly save it and reread after. Im thinking now about first language and c seems to have some graphical tutorials. cant help myself now finding something ultra structural like c books. so i think its a shaky ground to step on for practical learning, wont ever use the features of this language i think with this amount of googling around and no knowledge behind. i think i must learn c first and then will try zig on top. i done something like 70% ziglings and now this giant build system just tells me its not so easy to use this c libs only after ziglings, trying to understand this libs usage just from wrappers is confusing also.

zig has very few features that c doesnt, infact I think c has more features.

Which to learn first is difficult to say, c has a lot more resources, but it is also very fragmented with a million ways to do things, even building c code. There are so many build systems each with their own language to learn and quirks to avoid.

If you are willing to figure things out, or ask for help here or in the zig discord. If you are willing to fix your code after a zig update, as its still unstable, but updates are also not very frequent, I would recommend zig first. Otherwise, go with c.

Zig has no historical baggage, there is less to learn regarding the language itself.

The heavier topic is learning about computers and systems, which you will need to learn with any low level language.
For which I also recommend zig to learn about, it is much clearer what is zig, and what isn’t, which sounds weird but sooo many c things are mis represented to be OS or computer things which is just not true. But you’d figure that out if you learnt zig after anyway.

2 Likes