compile to get .cpp and .h files and linking them with other C++ program

339 views
Skip to first unread message

Aditya Ghantasala

unread,
Oct 22, 2014, 10:36:32 AM10/22/14
to cython...@googlegroups.com
Hello People

I am very new to cython. From what I understood, I wrote a small .pyx file with some cdef public variables and cdef public functions.
Then I used cython --cplus myScript.pyx  and get a .h and .cpp files.
Now I want to use these functions in an another .cpp file by including .h file.

First question is ... is it possible to do it ?
I tried to compile it and am stuck with the compiler complaining that it expects a constructor and destructor.  before the functions.

I am attaching my files with this post. Any suggestions to make it working .. ??
I am using python 3.3

Thanks in advance and Regards
Aditya.
cy_mod.pyx
cy_exp.cpp
cy_mod.cpp
cy_mod.h
cy_mod.pyx

Chris Barker

unread,
Oct 22, 2014, 8:07:12 PM10/22/14
to cython-users
On Wed, Oct 22, 2014 at 7:36 AM, Aditya Ghantasala <shine....@gmail.com> wrote:
I am very new to cython. From what I understood, I wrote a small .pyx file with some cdef public variables and cdef public functions.
Then I used cython --cplus myScript.pyx  and get a .h and .cpp files.
Now I want to use these functions in an another .cpp file by including .h file.

Cython quite specifically generates code for Python extensions. While it that process there may well be generally purpose C++ functions in there, it takes some care to get anything useful outside of a python extension. i.e. it isn't really a generic Python to C translator (though that would be pretty cool! )

If you want your other code to be part of a python extension, then best to use Cython itself to call that code, and it will take care of getting iot to work together.

If you're not sure what I mean, then perhaps you could post a very specific (probably simplified) example of what you are trying to do.

-Chris




 



 
First question is ... is it possible to do it ?
I tried to compile it and am stuck with the compiler complaining that it expects a constructor and destructor.  before the functions.

I am attaching my files with this post. Any suggestions to make it working .. ??
I am using python 3.3

Thanks in advance and Regards
Aditya.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Aditya Ghantasala

unread,
Oct 23, 2014, 4:02:06 AM10/23/14
to cython...@googlegroups.com
Hello Chriss ...
Thanks for the reply.

Actually I did not get what you explained. I will read more to understand.
But as you suggested I am giving you a simple example:

myModule.pyx
---------------------------------------------
cdef public double returnPlusTen(double arg)
         return arg+10.0
---------------------------------------------

Now I do : "cython  -3 --cplus myModule.pyx "
and get myModule.cpp and myModule.h files.

cplusExp.cpp
----------------------------------------------
#include "myModule.h"
#include "Python.h" // From python3.3m
void main (){

double k = 10.5;
double result = returnPlusTen(k);
printf("The result obtained is :: %f",result);

}
---------------------------------------------------------
I will compile the cplusExp.cpp with g++ by linking with -lpython3.3 flag.
It produces error saying "in myModule.h : expected constructor and destructor before returnPlusTen(double)"


Thanks in advance and Regards
Aditya

Stefan Behnel

unread,
Oct 23, 2014, 4:30:13 AM10/23/14
to cython...@googlegroups.com
Aditya Ghantasala schrieb am 23.10.2014 um 10:02:
> Actually I did not get what you explained. I will read more to understand.
> But as you suggested I am giving you a simple example:
>
> myModule.pyx
> ---------------------------------------------
> cdef public double returnPlusTen(double arg)
> return arg+10.0
> ---------------------------------------------
>
> Now I do : "cython -3 --cplus myModule.pyx "
> and get myModule.cpp and myModule.h files.
>
> cplusExp.cpp
> ----------------------------------------------
> #include "myModule.h"
> #include "Python.h" // From python3.3m
> void main (){
>
> double k = 10.5;
> double result = returnPlusTen(k);
> printf("The result obtained is :: %f",result);
>
> }
> ---------------------------------------------------------
> I will compile the cplusExp.cpp with g++ by linking with -lpython3.3 flag.
> It produces error saying "in myModule.h : expected constructor and
> destructor before returnPlusTen(double)"

To use a Cython extension module in an actual (directly runnable) C++
application, you need to embed the CPython runtime in that program. Here's
an example:

https://github.com/cython/cython/tree/master/Demos/embed

Cython generated code is not meant to be used stand-alone.

Stefan

Reply all
Reply to author
Forward
0 new messages