#(Solved) Trying to cross-compile a C library with zig cc (CMake)

1 messages · Page 1 of 1 (latest)

gusty gate
#

The error message is pretty clear: program 'x86_64-windows-gnu-windres' has not been found

It's a program responsible for handling .rs (resource) files on windows. I haven't played with it much but as far as I can tell (or this artice can https://www.ryanliptak.com/blog/zig-is-a-windows-resource-compiler/), zig is able to process these

frosty pine
#

(Solved) Trying to cross-compile a C library with zig cc (CMake)

round urchin
#

Is CMake necessary to compile C code with Zig? Or optional?

past charm
primal heron
#

if the thing you're compiling is open source, i'd appreciate a link to it so i can use it to check that zig windres works as a drop-in replacement for it (when i finish it)

primal heron
#

might be related to whether or not cmake thinks it's using mingw, if you can provide a reproduction i'll take a look

primal heron
#

ah, ok i ran into this as well but am only now realizing what was actually happening:

  • the linker has an implementation of 'cvtres' which is responsible for converting .res files into COFF .obj files before linking
  • when CMake generates the Makefile, it adds an intermediary step where it combines object files using ar before passing the result to the linker
  • llvm-ar and zig ar don't have the 'cvtres' capability, so this step fails with unknown file type for the .res file

i accidentally got around this by generating ninja build files instead of Makefiles, which doesn't do the object combining step and therefore passes the .res to the linker, which works

windres is unaffected by this because the ".res" it generates is actually a COFF object file that CMake uses the wrong extension for (it tells windres to generate an object file by passing -O coff, but keeps the name as .res)

#

in terms of immediate solutions:

  • use -G Ninja from cmake and ninja as your build tool
  • use windres instead of zig rc
  • use a built-from-source version of resinator and then set RC="resinator /:output-format coff" (in theory you could also do zig rc /:output-format coff but unfortunately i didn't include COFF object file creation in zig rc since i didn't realize it was useful here; I might reconsider that)
  • use a built-from-source version of resinator + a wrapper script that includes windres in its filename and executes resinator windres "$@"
    • note, though, that the future of the windres subcommand in resinator is uncertain; i added it because i was intending on adding a zig windres subcommand but that's no longer the plan (see https://github.com/ziglang/zig/pull/22522)

the best solution would be for CMake to recognize that .res files need to be passed to the linker and not to ar, or for CMake to allow for a 'cvtres' step, or something like that. I'll have to think about it more and will hopefully contribute something to CMake for this use case