Call the code generated by Cython from C / C ++ (Windows version)

ToDay

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)

environment

--Windows is used as the OS because Visual Studio is used.

Creating an environment with Visual Studio

Solution creation

project.png

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 code to source file

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.

Know the location of Python

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. pythonhome.png

Make a note of the path of this directory as it will be used next.

Property editing

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.

propety.png

Introduction of debug binaries and debugging symbols

Get the installer from the official Python homepage and launch the installer. Click Customize installation and then click the Next button. install.png next.png debug.png

Check download debug binaries, debugging symbols. If you do not use this Python as the main, you do not need to add PATH.

Build check

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.

Cython code creation

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

Add generation code

Generated at this time Add cytest.c cytest.h to the Visual Studio source and header files respectively.

Edit Source.cpp

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.

vstotal.png

When executed, it will be as follows.

result.png

Finally

It's hard ... As a reference

I will mention.

Recommended Posts

Call the code generated by Cython from C / C ++ (Windows version)
Call the code generated by Cython from C / C ++ (Mac version)
Call a command from Python (Windows version)
Call C from Python with DragonFFI
Call popcount from Ruby / Python / C #
Execute Python code from C # GUI
Get the address from the zip code
Call C / C ++ from Python on Mac
Call c language from python (python.h)
Download the file by specifying the download destination with Python & Selemiun & Chrome (Windows version)
[Implementation example] Read the file line by line with Cython (Python) from the last line
Install by specifying the version with pip
Read the file by specifying the character code.
Make the library created by Eigen in C ++ available from Python with Boost.Numpy.
[2021 version] From Selenium Basic installation to Web scraping execution by Python Windows 10 (64bit)