Hello! :wave: I was able to successfully follow the Cranelift JIT demo to get a working Cranelift JIT hello world example, but I'm having trouble finding a place to look to make a similar "hello world" example for ahead-of-time compilation. I tried using someone's example Brainfuck compiler, but I got linking errors on all my machines (x86 WSL2, ARM64 macOS, and i386 Linux) so I'm not exactly sure how to adapt it so it'll work. I assume there already exists some simple example for producing a static binary using Cranelift; where can I find it?
Are you passing -lc
when linking? And are you defining a main
function which you mark as Linkage::Export
?
@bjorn3 sorry for being slow to respond; thankfully the cranefuck author helped me figure out how to get linking working though!
Wait, why are you directly using ld instead of using gcc or clang? Gcc and clang pass a bunch of flags to the linker to tell it where to find system libraries, which libraries are unconditionally required and so on.
Pretty much everyone on Unix systems uses gcc, clang or whatever C compiler is the default for the platform instead of directly running the linker.
This is both a lot more portable and much simpler than reproducing all this logic for every OS you want to target.
@Sam Estep
@bjorn3 that makes a lot of sense, sounds like I should be using gcc or clang instead! More generally, I'd like to distribute my compiler as a standalone binary (via musl etc) which itself can produce standalone binaries, so I don't really want to just assume that the user already has gcc or clang installed on their system. What should I do in this case? For instance, it seems like the Rust compiler itself comes with a linker, but I assume that's because Rust bundles LLVM. Is there a way I can achieve this same UX but with Cranelift?
Do you mean rust-lld? On all Unix systems we (rustc) actually tell gcc/clang to use rust-lld as linker rather than directly invoking rust-lld.
OK gotcha; so, what happens when someone installs Rust but doesn't already have gcc or clang installed?
You get an error that gcc or clang was not found.
What about on Windows?
Sounds like you're saying I essentially can't just distribute one standalone binary for my compiler and also produce standalone binaries from user programs, is that accurate?
For wasm and embedded systems without OS rustc does directly invoke rust-lld as for those there is no libc and such.
For MSVC Windows by default we expect link.exe in the PATH, but can run rust-lld in link.exe mode. Even so you need the Windows SDK installed (which we legally can't redistribute) for import libraries and a couple of things that get statically linked into the executable. For MinGW Windows we ship a copy of MinGW.
Last updated: Dec 23 2024 at 12:05 UTC