title: How can I type "cargo run" without needing to set the LD_LIBRARY_PATH shell variable?
body: <p>I build a Rust program that calls a C++ function via a C interface. In order to execute the program, I have to run:</p>
  
  <pre class="lang-none prettyprint-override"><code>export LD_LIBRARY_PATH=&lt;path to shared c lib&gt;
  </code></pre>
  
  <p>or I get an error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
  </code></pre>
  
  <p>I tried to set the variable in a build script using <code>std::process::Command</code></p>
  
  <pre><code>Command::new("sh").arg("export").arg("LD_LIBRARY_PATH=&lt;path to shared c lib&gt;");
  </code></pre>
  
  <p>Although the command executes without an error, the variable is not set. How can I set the variable from my program while it is being executed?</p>
  
  <p>To be more concrete, I want to type only this:</p>
  
  <pre class="lang-none prettyprint-override"><code>cargo run
  </code></pre>
  
  <p>instead of </p>
  
  <pre class="lang-none prettyprint-override"><code>export LD_LIBRARY_PATH=&lt;path to shared c lib&gt;
  cargo run
  </code></pre>
  
  <p>My code so far:</p>
  
  <p>main.rs</p>
  
  <pre><code>/*---compile all with---
      g++ -c -fpic foo.cpp
      gcc -c -fpic test.c
      g++ -shared foo.o test.o -o libtest.so
  
      in order to execute we have to set the variable
      export LD_LIBRARY_PATH=/home/jan/Uni/Bachelorarbeit/Programme/Rust_Cpp_crossover_erneut/$LD_LIBARY_PATH
  
  */
  //pub extern crate c_interface;
  pub extern crate libc;
  use libc::{c_int};
  
  
  
  #[link(name = "test")]
  extern "C" {
      fn hello_world () -&gt; c_int;
  }
  
  
  fn main() {
      let x;
      unsafe {
          x = hello_world();
      }
      println!("x is: {}", x);
  }
  </code></pre>
  
  <p>test.c</p>
  
  <pre class="lang-c prettyprint-override"><code>#include "foo.h"
  
  int hello_world () {
      int a = foo();
      return a;
  }
  </code></pre>
  
  <p>foo.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include "foo.h"
  
  using namespace std;
  
  int foo() {
      cout &lt;&lt; "Hello, World!" &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>foo.h</p>
  
  <pre class="lang-c prettyprint-override"><code>#ifdef __cplusplus
  extern "C" {
  #endif
  
  int foo();
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>build.rs</p>
  
  <pre><code>fn main () {
      println!(r"cargo:rustc-link-search=native=/home/jan/Uni/Bachelorarbeit/Programme/Rust_Cpp_crossover_erneut");
  }
  </code></pre>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/40833078/how-do-i-specify-the-linker-path-in-rust">How do I specify the linker path in Rust?</a> and it is not a solution to my problem.</p>