Hi,
I am using nimble and I am trying to get cross compiling working using the following script that I found on stackoverflow, based on a debian docker container:
#!/bin/bash
mkdir -p /workdir/windows
cp -Rp * /workdir/windows/
rm -rf /workdir/windows/src/nimcache
cd /workdir/windows
nim c --cpu:amd64 --os:windows --opt:speed --embedsrc --threads:on --checks:on -c -d:release src/*.nim
cd src
cp /opt/Nim/lib/nimbase.h nimcache/
x86_64-w64-mingw32-gcc --save-temps nimcache/*.c -o windows.exe
That works absolutely fine, however, adding a new requires to project.nimble:
# Package
version = "0.1.0"
author = "Colin Yates"
description = "Chain Breaker"
license = "MIT"
srcDir = "src"
bin = @["chainbreaker"]
# Dependencies
requires "nim >= 0.18.0"
requires "zip" # NEW LIB
breaks the gcc step because the dependant nim files (in ~/.nimble/pkgs/zip/...~) don't appears to be compiled.
nim compilation works but the mingw32-gcc fails because it can't find the zip source files.
To be explicit, nimble build works fine, but obviously only builds for the current platform.
I am not sure if idiomatic cross-compilation means teaching nimble about platform specific stuff, or teaching nim to also include the ~/.nimble/pkgs/.
Help :-).
Never mind, the awesome @dom96 has already told me once: https://forum.nim-lang.org/t/4014 :-).
The solution is that nimble supports changing the compilation target.
Nope, I was too optimistic :-), I now get the following error on nimble c --cpu:amd64 --os:windows --opt:speed --embedsrc --threads:on --checks:on -c -d:release src/chainbreaker.nim
zip_zipfiles.o:zip_zipfiles.c:(.text+0xab): undefined reference to `zip_open'
zip_zipfiles.o:zip_zipfiles.c:(.text+0x408): undefined reference to `zip_strerror'
zip_zipfiles.o:zip_zipfiles.c:(.text+0x575): undefined reference to `zip_source_file'
zip_zipfiles.o:zip_zipfiles.c:(.text+0x5b2): undefined reference to `zip_add'
zip_zipfiles.o:zip_zipfiles.c:(.text+0x5c7): undefined reference to `zip_source_free'
zip_zipfiles.o:zip_zipfiles.c:(.text+0x5f6): undefined reference to `zip_close'
collect2: error: ld returned 1 exit status
You just need the full path to x86_64-w64-mingw32-gcc and nimble on the $PATH everything else is not required.
Example:
nim c --cpu:amd64 --os:windows --gcc.exe:/usr/bin/x86_64-w64-mingw32-gcc --gcc.linkerexe:/usr/bin/x86_64-w64-mingw32-gcc hello.nim
Thanks @juancarlospaco, but that didn't help.
I have put together a (Docker based) reproducible case if you fancy having a look at: https://github.com/yatesco/docker-nim-dev-example
Okay, so the issue is that you're trying to compile the .c code yourself using gcc. But whatever the Nim compiler does seems to be working fine.
I would suggest taking a look at nimcache/docker_nim_dev_example.json, it will contain information about how to compile the C sources. That should help you out. If this doesn't help you can resort to strace'ing the Nim process to see what gcc process it runs :)
That said, I would suggest not doing it this way. I'm not 100% sure why you're doing it this way, but AFAIK cross-compiling can be accomplished using nim only. You can tell it what flags to pass to gcc, what gcc to use, etc. This might help: https://forum.nim-lang.org/t/3487
Thanks @dom96. I am definitely stumbling around in the dark :-).
The reason I used nimble was because of your comment in https://forum.nim-lang.org/t/4014 ;-). I also thought nim would struggle to find the dependencies?
I guess I have a bit more reading to do!