title: Using a struct from a different library in Cython
body: <p>I am trying to call a C function in Cython and the header looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;inttypes.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;math.h&gt;
  
  #include &lt;apriltag.h&gt;
  #include &lt;tag36h11.h&gt;
  
  #include &lt;common/getopt.h&gt;
  #include &lt;common/image_u8.h&gt;
  #include &lt;common/image_u8x4.h&gt;
  #include &lt;common/pjpeg.h&gt;
  #include &lt;common/zarray.h&gt;
  
  apriltag_detection_t* scan_frame(int width, int height, uint8_t* data);
  </code></pre>
  
  <p>As you can see, I want to return an array of structs, which are type defined to be <code>apriltag_detection_t</code>. According to the <a href="http://cython.readthedocs.io/en/latest/src/tutorial/clibraries.html" rel="nofollow noreferrer">documentation</a>, in order to be able to use this in Cython, I must define some sort of <code>pxd</code> file which is essentially a copy of header.</p>
  
  <p>However, <code>apriltag_detection_t</code> is a type that is already defined in <code>apriltag.h</code>. Furthermore, <code>apriltag_detection_t</code> has members that are already defined in <code>apriltag.h</code>. Do I have to recursively redefine all of these types (by hand) in the Cython file before being able to use this library? Where would I write them?</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE 6</strong></p>
  
  <p>Finally at the step of wrapping a function!</p>
  
  <pre><code>from libc.stdint cimport uint8_t
  
  cdef extern from "&lt;apriltag.h&gt;":
      cdef struct apriltag_detection:
          int id
          double c[2]
          double p[4][2]
  
      ctypedef apriltag_detection apriltag_detection_t
  
  cdef extern from "tag36h11_detector/tag36h11_detector.h":
      apriltag_detection_t* scan_frame(int width, int height, uint8_t* data);
  
  def detect(width, height, frame):
      return scan_frame(width, height, frame)
  </code></pre>
  
  <blockquote>
    <p>tag36h11_detector.pyx:15:21: Cannot convert 'apriltag_detection_t *' to Python object</p>
  </blockquote>
  
  <p><code>apriltag_detection_t*</code> is meant to be an array of structs</p>
  
  <p><strong>UPDATE 5</strong>
  This seems to have worked.</p>
  
  <pre><code>from libc.stdint cimport uint8_t
  
  cdef extern from "&lt;apriltag.h&gt;":
      cdef struct apriltag_detection:
          int id
          double c[2]
          double p[4][2]
  
      ctypedef apriltag_detection apriltag_detection_t
  
  cdef extern from "tag36h11_detector/tag36h11_detector.h":
      apriltag_detection_t* scan_frame(int width, int height, uint8_t* data);
  </code></pre>
  
  <p><strong>UPDATE 4</strong>
  Solved the previous issues by importing the necessary types.</p>
  
  <pre><code>from libc.stdint cimport uint8_t
  
  cdef extern from "apriltag.h":
      cdef struct apriltag_detection:
          int id
          double c[2]
          double p[4][2]
  
      ctypedef apriltag_detection apriltag_detection_t
  
  cdef extern from "tag36h11_detector.h":
      apriltag_detection_t* scan_frame(int width, int height, uint8_t* data);
  </code></pre>
  
  <blockquote>
    <p>tag36h11_detector.c:533:10: fatal error: 'apriltag.h' file not found</p>
  </blockquote>
  
  <p>I'm not sure where this is coming from because my header file, as provided in the original post, required <code>&lt;apriltag.h&gt;</code> and not <code>"apriltag.h"</code>. This is what my <code>setup.py</code> looks like.</p>
  
  <pre><code>from distutils.core import setup, Extension
  from Cython.Build import cythonize
  
  setup(ext_modules=cythonize(Extension(\
      name='tag36h11_detector', \
      sources=["tag36h11_detector.pyx", \
      "tag36h11_detector/tag36h11_detector.c"], \
      include_path=["/usr/local/include/apriltag"], \
      libraries=["apriltag"])))
  </code></pre>
  
  <p><strong>UPDATE 3</strong></p>
  
  <pre><code>cdef extern from "apriltag.h":
      cdef struct apriltag_detection:
          int id
          double c[2]
          double p[4][2]
  
      ctypedef apriltag_detection apriltag_detection_t
  
  cdef extern from "tag36h11_detector.h":
      apriltag_detection_t* scan_frame(int width, int height, uint8_t* data);
  </code></pre>
  
  <blockquote>
    <p>tag36h11_detector.pyx:10:60: 'uint8_t' is not a type identifier</p>
  </blockquote>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>This is my current code and the following is the compilation error</p>
  
  <pre><code>// tag36h11_detector.pyx
  cdef extern from "apriltag.h":
      ctypedef apriltag_detection_t:
          int id
          double c[2]
          double p[4][2]
  
  cdef extern from "tag36h11_detector.h":
      apriltag_detection_t* scan_frame(int width, int height, uint8_t* data);
  
  // apriltag.h
      ...
      typedef struct apriltag_detector apriltag_detector_t;
      ...
  </code></pre>
  
  <blockquote>
    <p>tag36h11_detector.pyx:2:33: Syntax error in ctypedef statement</p>
  </blockquote>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>So I'm trying to interface with the above header file with Python (which I wrote and implemented) with types defined in <code>apriltag.h</code> (from a library).</p>
  
  <pre><code>cdef extern from "apriltag.h":
      struct apriltag_detection:
          int id
          double c[2]
          double p[4][2]
  
  cdef extern from "tag36h11_detector.h":
      struct apriltag_detection* scan_frame(int width, int height, uint8_t* data);
  </code></pre>
  
  <p>When I try compiling the above, I get</p>
  
  <blockquote>
    <p>tag36h11_detector.pyx:8:29: Syntax error in struct or union definition</p>
  </blockquote>