Introducing how to use Cython as a way to call Python from C / C ++.
It is common to write Sotobori in Python and call the core part C / C ++ for speeding up. This time it is ** reverse **. On the contrary, there is not much clue other than the official document ... Please let me know if you have any. OTL.
(Addition: I wrote the Mac version.)
-Call the code generated by Cython from C / C ++ (Mac version)
--Windows is used as the OS because Visual Studio is used.
Visual C ++-> Select an empty project-> Press the OK button for the time being. Now you can create an empty project. The solution and the name of the first project created is Project1.
Add an empty C ++ file with Ctrl + Shift + A. The default name is Source.cpp, but use it as it is. We will write the code in Source.cpp later. For now, leave it blank.
Please note that from here on, it will depend on your environment.
I think readers are familiar with Python, so you know which Python you are using. But let's check.
Open a command prompt
> where python
Is executed. In my case it was C: \ ProgramData \ Miniconda3 \ python.exe
.
Go to C: \ ProgramData \ Miniconda3
.
In the picture below, ʻinclude and
libs` are selected.
Make a note of the path of this directory as it will be used next.
Add the full path of include noted above to Properties> VC ++ Directory> Include Directory.
In my case C: \ ProgramData \ Miniconda3 \ include
is.
Add the full path of libs noted above to Properties> VC ++ Directory> Include Directory.
In my case C: \ ProgramData \ Miniconda3 \ libs
is.
Let's make it look like the red frame surrounded by the figure below. Also, set the platform to x64.
Get the installer from the official Python homepage and launch the installer. Click Customize installation and then click the Next button.
Check download debug binaries, debugging symbols. If you do not use this Python as the main, you do not need to add PATH.
Write the following in Source.cpp
source.cpp
#include<Python.h>
int main() {
Py_Initialize();
printf("Hello");
Py_Finalize();
return 0;
}
If you can build in release mode, it's OK for the time being. If you get angry that Python.h is missing, adding the include directory has failed. Let's calm down and set it again.
And in debug mode, you need a DLL with the same name as python35_d.lib, so you need to get it.
You can get it by starting the Python3 installer from the official Python page and selecting "download debug binaries" as an option, referring to the above stack overflow. Let's find it.
Create the following in the same layer as Source.cpp created above.
Each code is as follows.
setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("cytest.pyx"))
cytest.pyx
from libc.stdio cimport printf
cdef public struct Point:
double x
double y
cdef public Point DoubleCoord(Point p):
cdef Point q
q.x=2*p.x
q.y=2*q.x
printf("q.x=%f, q.y=%f\n",q.x,q.y)
return q
For the time being, I made a function in Cython that defines a vertex structure and doubles the coordinates and returns it.
setup Open a command prompt and build your Cython code.
> python setup.py build_ext --inplace
Generated at this time Add cytest.c cytest.h to the Visual Studio source and header files respectively.
Source.cpp
#include<Python.h>
#include<stdio.h>
#include "cytest.h"
int main() {
Py_Initialize();
struct Point p;
p.x = 3.5;
p.y = 7.0;
struct Point q = DoubleCoord(p);
printf("q.x=%f,q.y=%f", q.x, q.y);
Py_Finalize();
}
As a result so far, it should be as shown in the figure below on Visual Studio.
When executed, it will be as follows.
It's hard ... As a reference
I will mention.
Recommended Posts