5

Due to how the LGPL works, compiling Rust code to an object file without linking would be useful to be able to do. However, I cannot find any documentation on how to do this. I checked rustc's help section and searched, but couldn't find anything, which brings me to my question: How do I tell rustc to not link and produce object files that later can be linked?

Newbyte
  • 2,421
  • 5
  • 22
  • 45

1 Answers1

7

Use the compiler flag --emit=obj.

cargo rustc -- --emit=obj

The compiled object files will be sitting in target/debug/deps.

See also:

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Hmm, this doesn't seem to work. I get a `warning: ignoring emit path because multiple .o files were produced` logged when running the command and `ls target/debug/deps | egrep '\.o'` yields nothing. Also looking at the results I get from `ls target/debug/deps` I don't see any .o files. – Newbyte Oct 07 '20 at 11:36
  • That is project dependent, @Newbyte. You may need to specify a single target crate at a time (e.g. only lib). – E_net4 Oct 07 '20 at 11:40
  • Would it be reasonable to add an example of how you'd go about doing that to the answer? I don't think I'm familiar enough with cargo and rustc to know how to do that, and I would think it's at the very least indirectly relevant to the question. – Newbyte Oct 07 '20 at 11:58