title: How to use a C typedef struct and functions with that struct from Rust?
body: <p>I have these C files and I want to use <code>pair_add</code>, <code>add</code> and <code>PAIR</code> from Rust.</p>
  
  <p><strong>adder.c</strong></p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdlib.h&gt;
  #include "adder.h"
  
  int pair_add(PAIR * ppair) {
      return ppair-&gt;x + ppair-&gt;y;
  }
  
  int add(int x, int y) {
      return x + y;
  }
  </code></pre>
  
  <p><strong>adder.h</strong></p>
  
  <pre class="lang-c prettyprint-override"><code>typedef struct {
      int x;
      int y;
  } PAIR;
  
  int pair_add(PAIR * ppair);
  int add(int, int);
  </code></pre>
  
  <p>I compile them using:</p>
  
  <pre class="lang-none prettyprint-override"><code>gcc -c adder.c
  ar rc libadder.a adder.o  # Static link
  </code></pre>
  
  <p>The <a href="https://doc.rust-lang.org/book/second-edition/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code" rel="nofollow noreferrer">documentation</a> does not detail how to integrate C typedef structs and <a href="https://doc.rust-lang.org/book/ffi.html" rel="nofollow noreferrer">the example</a> is for functions which return and accept <code>i32</code>. <a href="https://github.com/alexcrichton/rust-ffi-examples/blob/master/rust-to-c/src/main.rs" rel="nofollow noreferrer">Other online resources were also limited</a>.</p>
  
  <p>I attempted the following but could not add the <code>PAIR</code> typedef:</p>
  
  <pre><code>extern crate libc;
  
  use libc::c_int;
  
  #[link(name = "adder")]
  extern "C" {
      // Define PAIR
  
      // int pair_add(PAIR * ppair);
      fn pair_add(input: Pair) -&gt; c_int;
  
      // int add(int, int);
      fn add(input1: c_int) -&gt; c_int;
  }
  
  fn main() {}
  </code></pre>