Python Boost Examples

“Boost is an interface between Python and C++ provided via the Python Language framework. It enables you to rapidly and easily provide C++ classes, methods, and entities to Py, and conversely, without the need for any further tools outside your C++ translator. Because Boost is intended to encapsulate C++ interfaces without modifying the C++ program, you shouldn’t have to do anything different to cover a C++ interface.”

It is best to expose third-party modules to Python using Python. Due to the obvious library’s use of sophisticated metaprogramming methods, encapsulating code seems to users to be a form of expressive interface design language, simplifying its semantics for them (IDL).

Before taking a look at the example, we need to set up the boost python in the Linux system. So, we have to download the tar file from the web in our home directory, as shown in the image below. It may take up to 15 minutes or more according to your system’s internet speed.

You need to extract this downloaded “tar” file within the same folder after the successful download. For this, you need to open your terminal console of the Linux system first. If you don’t know the way to do so, just try out the Ctrl+AlT+T, and the terminal will be launched in a few seconds.

Within the terminal query area, you need to utilize the “tar” query along with the “-xf” option followed by the full name of a file, i.e., including its extension “tar.gz”. The use of “-xf” means that this file has to be extracted forcefully without interruption or issue. It will be extracted in seconds. You can sit down all the files and folders of the current working directory to see whether it has been extracted successfully or not.

Now, we will be moving within the extracted boost folder to see its contents. For this, we have been utilizing the “Cd” instruction along with the boost folder full name, and we are successful. We have tried the list “ls” instruction to list down all its subfiles and sub-folders on the shell screen. On listing, we have been shown different bash files, some html, html, and jam files as well.

To build the engine of BOOST python in the Linux system, we need to utilize the bash file named “” on the query area to execute it with “./” characters at the start. This file can be seen as the green highlighted text within the listed files and folders of the boost extracted folder at our console. It has started to build the Boost module via the C toolset, i.e., the C compiler named GCC. It might take up to 1 minute to complete this process and to move toward the next step of this article.

The very next step in this process is to make configuration within the particular folder of the Linux system. For this, you need to execute the “b2” file from the above-listed files and folders within the installation command. We need to use the sudo rights for the execution of this instruction, along with the path to the folder where you want to configure it. In our case, that folder would be the “/usr” folder of our Linux operating system.

The very same command can be executed within the specification of a configuration path. The use of this installation instruction may take up to 5 minutes to get the execution done.

It’s time to create 3 new files within the boost extracted folder or the home folder of your system. So, we have been using the touch instruction 3 times to create one C++ file, one python, and one cmake file. Make sure you have the python interpreter and C++ compiler installed at your end along with the cmake module. The cmake file must contain the “Txt” extension, and file names for the other two files can differ.

Within the cmake text type file format, we have been specifying the version of python we have been utilizing for this article implementation. The FIND_PACKAGE variable function has been used to find out the python Interpreter, its libraries, and its boost components of python. Along with that, you need to include the required directories of python and Boost via the use of INCLUDE_DIRECTORIES.

In the very next line, we have been using the PYTHON_ADD_MODULE() to utilize the NativeLib module in the CProj C++ file, i.e., will be shown after this. We have been using the FILE() function variable to copy the Project.Py file to the destination. Save this code now.

It’s time to take a look at the C++ file named “CProj” in this article. We have created a constant character type pointer function named “Welcome” within this code. It returns some message to its calling function. We have been utilizing the BOOST_PYTHON_MODULE along with the name of a C++ project file without its extension. Within this BOOST_PYTHON_MODULE() function, we have been utilizing the boost package of python to define the Welcome function and pass it the Welcome function as an argument.

The python file named “” has been started with the import of the NativeLib module via the use of the import keyword. At the very next line, we have been calling the Welcome function from the above C++ file using the NativeLib module in the print statement displayed.

Now, we have been creating a new build folder within the boost extracted folder with the use of the mkdir query.

After this, we have been using the cmake query to build the type of a cmake. After this, try out the make command followed by the execution of a python file.


Going through this process, you will be able to use the C++ functions, libraries, and packages within the python code. We have discussed how you can make Boost enable on Ubuntu 20.04 Linux operating system. All the steps are very easy to learn and implement. This article can be implemented on any Linux distribution.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content