title: Compiling Rust static library and using it in C++: undefined reference
body: <p>I'm trying to compile a <code>static</code> library in Rust, and then use it in my C++ code (note this is about calling Rust from C++ and not the other way around). I went over all the tutorials I could find online, and replies to similar questions, and I'm obviously doing something wrong, though I can't see what.</p>
  <p>I created a minimal example for my problem :</p>
  <p><strong>1. Cargo.toml :</strong></p>
  <pre><code>[package]
  name = &quot;hello_world&quot;
  version = &quot;0.1.0&quot;
  
  [lib]
  name = &quot;hello_in_rust_lib&quot;
  path = &quot;src/lib.rs&quot;
  crate-type = [&quot;staticlib&quot;]
  
  [dependencies]
  </code></pre>
  <p><strong>2. lib.rs :</strong></p>
  <pre><code>#[no_mangle]
  pub unsafe extern &quot;C&quot; fn hello_world_in_rust() {
      println!(&quot;Hello World, Rust here!&quot;);
  }
  </code></pre>
  <p><strong>3. hello_world_in_cpp.cpp :</strong></p>
  <pre><code>extern void hello_world_in_rust();
  
  int main() {
      hello_world_in_rust();
  }
  </code></pre>
  <p>To build the library, in my rust directory I ran :</p>
  <blockquote>
  <p>cargo build --lib</p>
  </blockquote>
  <p>(which went fine)
  I proceeded to run, in my C++ folder :</p>
  <blockquote>
  <p>clang++ hello_world_in_cpp.cpp -o hello.out -L ../hello_world/target/release/ -lhello_in_rust_lib</p>
  </blockquote>
  <p>Which resulted in the following error :</p>
  <blockquote>
  <p>/tmp/hello_world_in_cpp-cf3577.o: In function <code>main</code> :</p>
  <p>hello_world_in_cpp.cpp:(.text+0x5): undefined reference to <code>hello_world_in_rust()</code></p>
  </blockquote>