3

I have been using Cython on a couple of projects. I see a lot of documentation on including C++ and C into your cython code. Sometimes when I am extending an existing C/C++ project I feel the urge to code it Python style taking advantage of all the nice features and datatypes. It would be really nice if there was an easy way to ie. call a cython compiled function from C++ or C. Is there an easy way to do this, cause I can't seem to find it in the docs. Or maybe I shouldn't use Cython for this??

Best regards Jakob

1 Answers1

1

I had a more constrained version of your problem and @fabrizioM's answer should work for you:


The trick with cython is in using the keyword public

cdef public double cython_function( double value, double value2 ):
    return value + value2

In this way you can link it directly

as a normal C library:

#ifdef __cplusplus {
extern "C"
#endif

double cython_function( double value, double value2 );


#ifdef __cplusplus {
}
#endif
Community
  • 1
  • 1
Adam
  • 16,808
  • 7
  • 52
  • 98
  • That's right. Moreover you'll have to `#include "../yourext_api.h"` file (generated by Cython when using the public keyword) in your C/C++ file to be able to use the `cython_function` from C side... – Gauthier Boaglio Oct 02 '13 at 09:17
  • braces `{}` seems to be misplaced. See [this question for comparison](http://stackoverflow.com/q/3789340/4279) – jfs Jan 09 '14 at 17:08