title: How do I expose a compile time generated static C string through FFI?
body: <p>I am trying to embed a version number into a library. Ideally, this should be a static C string that can be read and doesn't need any additional allocation for reading the version number.</p>
  
  <p>On the Rust side, I am using <code>vergen</code> to generate the versioning information like this:</p>
  
  <pre><code>pub static VERSION: &amp;str = env!("VERGEN_SEMVER");
  </code></pre>
  
  <p>and I would like to end up with something like</p>
  
  <pre><code>#[no_mangle]
  pub static VERSION_C: *const u8 = ... ;
  </code></pre>
  
  <p>There seems to be a way to achieve this using <a href="https://stackoverflow.com/questions/29563785/how-do-i-create-static-c-strings">string literals</a>, but I haven't found a way to do this with compile time strings. Creating a new <code>CString</code> seems to be beyond the current capabilities of static variables and tends to end with an <a href="https://doc.rust-lang.org/1.2.0/error-index.html#E0015" rel="nofollow noreferrer">error E0015</a>. </p>
  
  <p>A function returning the pointer like this would be acceptable, as long as it does not allocate new memory.</p>
  
  <pre><code>#[no_mangle]
  pub extern "C" fn get_version() -&gt; *const u8 {
      // ...
  }
  </code></pre>
  
  <p>The final type of the variable (or return type of the function) doesn't have to be based on <code>u8</code>, but should be translatable through <code>cbindgen</code>. If some other FFI type is more appropriate, using that is perfectly fine.</p>