Python meets C

Python becomes more and more popular. It has lots of libraries one can use. But if you need to do complex calculations it’s not very effective. Better approach would be create CPU consuming part in C/C++ as a library and use it in Python. So let’s see how it could be done.

We will start with creating library in C. Follow this post to setup environment on Windows (install Visual Studio Tools 2019). Then follow this post to install Python. For Windows we have two options 32-bit or 64-bit. Please remember your choice (sample source code is setup for 64-bit).

Source code could be found here. Clone the repository and for to Win/part1 folder.

Now depending on Python bitness we have build either 32-bit library or 64-bit library. To build 64-bit library go to Start menu -> Visual Studio 2019 -> x64 Native Tools Command Prompt for VS 2019. To build 32-bit library go to Start menu -> Visual Studio 2019 -> Developer Command Prompt for VS 2019; also small change is needed for 32-bit in a makefile: linker flags should NOT include /MACHINE:X64. Now run


if all was setup correctly our c library should be compiled and linked and python test script should be executed:

I won’t go into details of Makefile. You can look for more details here and here what Makefiles are.

to be continued…

Leave a Reply