Skip to content

Instantly share code, notes, and snippets.

@birkin
Last active February 28, 2024 21:47
Show Gist options
  • Save birkin/567090f41b24a3817cf5c159df7bf17a to your computer and use it in GitHub Desktop.
Save birkin/567090f41b24a3817cf5c159df7bf17a to your computer and use it in GitHub Desktop.
cross-compilation question (mac to redhat-linux)

(vacation-day experimentation)

(i posted an abbreviated version of this question to Nim-Discord)

I've been experimenting with nim and am really enjoying it.

Now I want to get compile a binary to run on a Red Hat 7.9 server, and haven't gotten that to work, and would love a few links to some favorite writeups.

Some info, in case it's useful:

  • I'm using a Mac M1, with nim installed via homebrew.

  • I can successfully compile a binary on my Mac, and run it.

  • I've tried nim c --os:linux --cpu:amd64 -o:./nim_hello_world_for_linux ./helloworld.nim -- which seems to compile fine, but when I copy the binary over to redhat and try to run it, I get the output:

    -bash: ./nim_hello_world_for_linux: cannot execute binary file

  • I've installed nim on a CentOS 7.9.x docker image, and run that same nim c ... command -- which again seems to compile fine, but again when I copy the binary over to redhat and try to run it, I get the same failure output-message.

  • ChatGPT led me through some troubleshooting. It said to run:

    readelf -l ./nim_hello_world_for_linux -- and to pay attention to the INTEROP output. I did that, and it yields:

    [Requesting program interpreter: /lib/ld-linux-aarch64.so.1] -- so it seems to be ignoring the --cpu:amd64 flag.

  • Lastly, I did read the cross-compiler docs at: https://nim-lang.org/docs/nimc.html#crossminuscompilation -- but don't have a sense of how to move forward. And I did perform a few nim-discord searches, but have missed possible answers.

Walk-through links or tips appreciated!


Answer from Elegantbeef...

  • % nimble install zigcc

  • I had already installed zig for experimentation, so the zig compiler was all set

  • I did this, based on a chatgpt suggestion, but it may not have been necessary: % export CC=zigcc

  • % nim c -d:release --cc:clang --clang.exe:zigcc --clang.linkerexe:zigcc --passC="-target x86_64-linux-gnu.2.19" --os:linux --passL="-target x86_64-linux-gnu.2.19" -o:./nim_hello_world_for_linux ./helloworld.nim


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment