5

I'm trying to run a .ll file with clang and getting linker errors. I have a file test.rs that simply includes a main function with a println! statement. I generate the LLVM IR with the command rustc --emit=llvm-ir --crate-type=bin test.rs. When I try to run the output test.ll file with clang test.ll I get a linker error:

Undefined symbols for architecture x86_64:
  "std::io::stdio::_print::h178318b95760562a", referenced from:
      rust_test::main::h84a9713c734a1b45 in rust_test-9ea667.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So I figured clang couldn't find the Rust libraries and attempted to provide a path to them with the command clang -L$HOME/.rustup/toolchains/<arch>/lib test.ll but I receive the same error.


The goal here is to create a couple functions in Rust that I will call from LLVM, so I'll have a custom file.ll that will use functions that the Rust LLVM IR will provide. I noticed that rustc has a crate-type command-line argument called staticlib and tried to use that as well but to no avail.

trent
  • 25,033
  • 7
  • 51
  • 90
Jon Catanio
  • 283
  • 1
  • 3
  • 11
  • You need to link Rust's libraries. See [this](https://stackoverflow.com/a/42626498/598057) for inspiration. – Stanislav Pankevich Jan 24 '18 at 19:53
  • Use `-l` flag to actually link libraries. – arrowd Jan 24 '18 at 20:08
  • @StanislavPankevich hmm, seems like that didn't work for me I did `clang -L/Users/JonCatanio/.rustup/toolchains//lib test.ll -lstd-826c8d3b356e180c` – Jon Catanio Jan 24 '18 at 22:34
  • *Why* use LLVM IR instead of just creating an object file and linking it as normal? – Shepmaster Jan 25 '18 at 14:49
  • 2
    @Shepmaster I was writing a library in Rust that provides an FFI allowing custom LLVM to call into Rust. I'm working on writing a compiler for Python in Rust and wanted to offload some of the work to a library so I don't have to generate a ton of LLVM. I realize now that I wasn't approaching it properly and found these as reference to help me: [tutorial](http://zsiciarz.github.io/24daysofrust/book/vol1/day23.html) [FFI info in the Rust book](https://doc.rust-lang.org/book/first-edition/ffi.html) – Jon Catanio Jan 26 '18 at 20:15

0 Answers0