acolite-d / llvm-tutorial-in-rust-using-inkwell

An implementation of Kaleidoscope, the LLVM tutorial model language, written in Rust using Inkwell.
6 stars 0 forks source link

Steps to actually run the compiled kaleidoscope binary #1

Open mhanberg opened 1 week ago

mhanberg commented 1 week ago

What are the steps to actually run the compiled file?

Running cargo run test.ks and then ./a.out results in zsh: exec format error: ./a.out for me.

Thanks!

acolite-d commented 1 day ago

Hi @mhanberg!

This compiler can only produce object/assembly files, not executables. You will need to link with your runtime and define entrypoints in order to turn this into a runnable executable, which was not part of the original LLVM tutorial IIRC. Executing code will require the interpreted session.

Also, be sure to use -- followed by the arguments to pass to your built binary, ex. cargo r -- --help or cargo r -- test.ks.

acolite-d commented 1 day ago

Actually, instead of linking with runtime and defining entrypoints, it might be easier to feed the resulting object into a C compiler of your choice, or rustc and run functions like so:

// test.c
#include <stdio.h>

extern double fib(double);
extern double fibi(double);

int main(void) {
    printf("The 20th fibonacci number is: %.0lf\n", fibi(20));
    return 0;
}
clang test.c a.out -o test 
kaleidrs$ ./test
The 20th fibonacci number is: 6765