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.

5
  • You need to link Rust's libraries. See this for inspiration. Commented Jan 24, 2018 at 19:53
  • Use -l flag to actually link libraries. Commented Jan 24, 2018 at 20:08
  • @StanislavPankevich hmm, seems like that didn't work for me I did clang -L/Users/JonCatanio/.rustup/toolchains/<arch>/lib test.ll -lstd-826c8d3b356e180c Commented Jan 24, 2018 at 22:34
  • Why use LLVM IR instead of just creating an object file and linking it as normal? Commented Jan 25, 2018 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 FFI info in the Rust book Commented Jan 26, 2018 at 20:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.