Part 1: Boost python with c/c++ bindings. Beginners’ friendly guide to start embedding c/c++ shared libraries in python.

Python is a very easy, but versatile programming language used almost everywhere. Being an interpreted language it naturally lags behind in terms of speed of execution.

In this tutorial you will learn on how to make your python program faster using c/c++ code. Let’s start.

Note: here are the requirements you need to meet:- Visual Studio 2019 (don’t confuse it with Visual Studio Code)- installed python- pycharm community edition- OS: windows 10

There are 3 things we need to do:

  1. create a c/c++ shared library in Visual Studio
  2. create python module to consume the c/c++ code (shared library)
  3. run and check how program works

1: create c/c++ shared library in Visual Studio

  • Add new DLL (Dynamic-Link Library) project in Visual Studio

Note: in windows shared library has extension .dll whereas on Linux it’s .so — here more info.

Now create two files with implementation given below:

  • test.h (header file)
  • test.cpp (cpp file)

Note: This is just an example of sum function implemented in c++ and used in python. In later tutorials we’re going to have more advanced use cases.

// test.h#pragma once
#include <string>
int sum(int, int);

Let’s analyse what’s in the test.h:

  • pragma once makes sure that the file is loaded only once at a precompiling step. Simply, c/c++ merge all source files into one file before compilation to binary code, and it makes sure there is only one copy of this file.
  • #include <string> this is a header from the source code. Simply, it allows using strings and its corresponding functions.
  • int sum(int, int) this is a very simple function declaration that returns integer value as a sum of two other integers.
//test.cpp#include “pch.h”
#include <string>
#define DLLEXPORT extern “C” __declspec(dllexport)DLLEXPORT int sum(int a, int b) {
return a + b;

Let’s analyse what’s in the test.cpp:

  • #include “pch.h” this is auto generated precompiled header needed for shared library (dynamic library) using Visual Studio. An explanation is far beyond this tutorial. One thing to remember is to make sure that #include “pch.h” is at the top of the file cause everything above will not compile (this is just how compilation works :) )
  • #include <string> please look above
  • #define DLLEXPORT extern “C” __declspec(dllexport) — define means that DLLEXPORT == extern “C” __declspec(dllexport) and now it may be used below in the code. extern c means that our function sum will have the same name in python program (in a nutshell). __declspec(dllexport) makes sure that the function will be visible to python.
  • int sum(int a, int b){ return a+b} this is just a function implementation of our function.

And now let’s compile our simple program. You need to click a green triangle at the middle top in Visual Studio.

We are done with c++!

Note: when you build your project make sure to have the same build configuration as python. For example if you have python 64 bit you need to build c/c++ on 64 bit as well. You can check your python build version on a python console at the bottom of the Pycharm:

2: create python module to consume the c/c++ code (shared library)

  • create new python project in the IDE
  • create python file with below implementation
from ctypes import *

if __name__ == "__main__":
mydll = cdll.LoadLibrary(r"C:\Users\PC\PycharmProjects\shared_library_example\your_project_name.dll")

print(mydll.sum(10, 10))

Let’s analyse what’s in the

  • from ctypes import * — is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
  • cdll.LoadLibrary — this function loads our shared library.
  • print(mydll.sum(10, 10)) — here we’re printing the sum of 10 and 10 defined in the shared library we’ve just loaded.

3: run and check how program works

To run python program we have two options:

  1. from IDE (example shown in pycharm)
  2. from terminal
  • run python program from a pycharm IDE

At the right top bottom there should a green triangle. Click it, and you will see the script output at the bottom of the pycharm IDE.

green triangle to start your program
  • from a terminal

Open a terminal (search for PowerShell in the windows).

Then type cd location/of/your/python/program like:

Then type python ./

In the next tutorial I will show a reverse — how to run python from c/c++ program.

Wish you best!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store