title: setuptools: build shared library from C++ code, then build Cython wrapper linked to shared library
body: <p>We have a bunch of C++ files with classes that we wrap to Python using Cython. We use setuptools to build the Cython extension. This all works fine, we followed the guide here:
  <a href="http://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html" rel="nofollow noreferrer">http://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html</a></p>
  <p>We are basically doing something like this</p>
  <pre><code>from distutils.core import setup
  from Cython.Build import cythonize
  
  setup(ext_modules = cythonize(
             &quot;rect.pyx&quot;,                 # our Cython source
             sources=[&quot;Rectangle.cpp&quot;],  # additional source file(s)
             language=&quot;c++&quot;,             # generate C++ code
        ))
  </code></pre>
  <p>We don't like about this that we have to recompile everything, even if only the Cython part changes, <code>rect.pyx</code> in this example. In fact we never touch the <code>.cpp</code> files, but change the <code>.pyx</code> files often.</p>
  <p>We would like to compile the <code>.cpp</code> files separately into a static or shared libary, then build <code>.pyx</code> files independently, which links to the library generated from the <code>.cpp</code> files. All this would be easy with <code>make</code> or <code>cmake</code>, but we want a pure Python solution that uses only <code>setuptools</code>. Mock-up code would look something like this:</p>
  <pre><code>from distutils.core import setup
  from Cython.Build import cythonize
  
  class CppLibary:
      # somehow get that to work
  
  # this should only recompile cpplib when source files changed
  cpplib = CppLibary('cpplib',
                     sources=[&quot;Rectangle.cpp&quot;], # put static cpp code here
                     include_dirs=[&quot;include&quot;])
  
  setup(ext_modules = cythonize(
             &quot;rect.pyx&quot;,                 # our Cython source
             libraries=[cpplib],         # link to cpplib
             language=&quot;c++&quot;,             # generate C++ code
        ))
  </code></pre>