0

So, I have been writing a compiler for a simple lisp using Rust and generating LLVM IR using the Inkwell crate. While trying to find a way to print values to standard output, I came across many answers for using print function just like in C/C++. It seems to work without any problem for the most part.

However, only the function declaration shows up in the generated IR, so it probably means the definition is being linked by llvm itself somewhere(using lli interpreter currently since it's much easier for testing). Anyway, I was just trying to understand where this function is defined. Like is there a core module in llvm where it is defined? Or is using printf dependent on the Unix-like platform instead of being an llvm thing since most llvm functions seem to have "llvm" prefix?

Ayush Singh
  • 1,409
  • 3
  • 19
  • 31

1 Answers1

2

printf is defined in the C standard library (libc).

So when you compile your llvm IR to object files and then link them, you'll have to link against a libc to be able to use printf (note that it's pretty common to link the object files generated by LLVM using a C compiler such as gcc or clang, which will link against a libc automatically).

When using lli, I believe you get access to any library that lli itself is linked against and that includes a libc.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Ok, I think I understand. I am guessing standard io will need to be defined differently for each platform for a cross-platform language? – Ayush Singh May 16 '21 at 14:59
  • 2
    @AyushSingh If you want to forgo a dependency on libc, then yes, you'll have to define your own version of IO primitives for each platform. That's not necessarily advisable though. Many language implementations depend on libc for IO and other core functions. I know Go switched from using syscalls to linking against libc on macos (and I believe it always used libc on Windows) because OS updates kept changing the syscalls in ways that broke the Go implementation, so libc is basically the only stable interface to core OS functionality (on Linux syscalls are stable though). – sepp2k May 16 '21 at 15:50