Boost python - Setup
Boost python setup
1. Integrating C++ and Python with Boost-Python: A Step-by-Step Guide -
Linux Saga
1. I. Python Compilation - Steps Involved
2. II. Boost Library - Steps Involved
3. III. C++ Code - Steps Involved
4. IV. C++ Code Compiling with CMake - Steps Involved
5. V. Python Code - Steps Involved
2. Boosting Python on Windows: A Step-by-Step Guide
1. 1. Installing Python
2. 2. Building the Boost Library
3. 3. Compiling C++ Code with CMake
Integrating C++ and Python with Boost-
Python: A Step-by-Step Guide - Linux Saga
This tutorial demonstrates how to integrate C++ code with Python using Boost-
Python. We'll create a simple C++ function, expose it to Python, and then call it
from a Python script.
I. Python Compilation - Steps Involved
This section details how to compile Python from source, giving you fine-grained
control over the installation process. While often unnecessary, this can be useful
for specific build requirements or when a desired version isn't readily available
through package managers.
1. Obtain the Python Source Code: Download your preferred Python version's
source code. This tutorial uses Python 3.11 as an example. You can typically
find source code on the official Python website or a mirror.
2. Extract the Source Code: Extract the downloaded archive to a dedicated
directory. This keeps your build environment organized.
3. Configure the Build: Navigate to the extracted source directory in your
terminal and run the configure script. This script prepares the build
environment and allows you to customize the installation. The example below
demonstrates a typical configuration:
1 ./configure \
2 --prefix=/home/raghav/Documents/playground/boost/py_build \
3 --enable-shared \
4 --enable-optimizations \
5 --enable-ipv6 \
6 LDFLAGS=-Wl,-
rpath=/home/raghav/Documents/playground/boost/py_build/lib,--
disable-new-dtags
--prefix : Specifies the installation directory. In this case, it's
/home/raghav/Documents/playground/boost/py_build .
--enable-shared : Builds Python as a shared library.
--enable-optimizations : Enables compiler optimizations.
--enable-ipv6 : Enables IPv6 support.
LDFLAGS : Sets linker flags, here specifying the runtime library path and
disabling new dynamic tags.
4. Compile Python: After configuring, compile the source code using the make
command.
5. Install Python: Install the compiled Python using make install . This
command copies the necessary files to the directory specified by the --
prefix option during configuration. In this example, Python will be installed
locally within your project directory, avoiding conflicts with system-wide
Python installations.
II. Boost Library - Steps Involved
This section explains how to build the Boost-Python library, which is crucial for
bridging C++ and Python.
1. Download and Extract Boost: Download the latest Boost library source from
the official website and extract it to a chosen directory.
2. Bootstrap the Build: Navigate to the extracted Boost directory and run the
[Link] script. This script prepares the Boost build system. Crucially,
you need to specify options to tailor the build for your Python installation:
1 ./[Link] --with-
python=/home/raghav/Documents/playground/boost/py_build/bin/python3.
\
2 --with-python-root=/home/raghav/Documents/playground/boost/py_build
3 --with-libraries=python \
4 --libdir=/home/raghav/Documents/playground/boost/lib \
5 --includedir=/home/raghav/Documents/playground/boost/include \
6 --with-toolset=gcc \
7 --with-python-version=3.11
--with-python : Path to your compiled Python executable.
--with-python-root : Path to your Python installation directory.
--with-libraries=python : Specifies that only the [Link] library
should be built.
--libdir and --includedir : Specify where the compiled Boost libraries
and headers will be installed.
--with-toolset : Specifies the compiler to use (GCC in this example).
--with-python-version : Specifies the Python version.
3. Compile and Install [Link]: Run ./b2 to compile the [Link]
library. After successful compilation, run ./b2 install to copy the compiled
libraries and header files to the directories specified by --libdir and --
includedir .
III. C++ Code - Steps Involved
This section explains how to write the C++ code that will be called from Python.
1. Include [Link]: Include the necessary Boost-Python header file:
1 #include <boost/[Link]>
2. Write Your C++ Function: Write the C++ function you want to expose to
Python. No main function is needed as this code will be compiled into a
library.
3. Expose the Function to Python: Use BOOST_PYTHON_MODULE to make the C++
function accessible from Python. This macro creates the necessary bindings:
1 // Function to be exposed to Python
2 int add(int a, int b) {
3 return a + b;
4 }
5
6 // Expose the function to Python
7 BOOST_PYTHON_MODULE(example) {
8 using namespace boost::python;
9 def("add", add);
10 }
This code exposes the add function to Python under the name "add".
IV. C++ Code Compiling with CMake - Steps
Involved
This section explains how to use CMake to compile the C++ code into a shared
library that Python can import.
1. Create a [Link] File: Create a file named [Link] in the
same directory as your C++ code.
2. Configure CMake: The [Link] file configures the build process. It
specifies the necessary include directories, libraries, and linker settings:
1 cmake_minimum_required(VERSION 3.12)
2 project(BoostPythonExample)
3
4 # Set the Python version (adjust to match your Python version)
5 set(PYTHON_VERSION 3.11)
6
7 # Specify paths to Boost and Python
8 set(BOOST_INCLUDE_DIR /home/raghav/Documents/playground/boost/includ
# Path to Boost headers
9 set(BOOST_LIBRARY_DIR /home/raghav/Documents/playground/boost/lib)
# Path to Boost libraries
10 set(PYTHON_INCLUDE_DIR
/home/raghav/Documents/playground/boost/py_build/include/python3.11)
# Path to Python headers
11 set(PYTHON_LIBRARY
/home/raghav/Documents/playground/boost/py_build/lib/libpython3.11.s
# Path to Python shared library
12
13 # Add the library
14 add_library(example SHARED [Link])
15
16 # Include directories for Boost and Python
17 target_include_directories(example PRIVATE ${BOOST_INCLUDE_DIR}
${PYTHON_INCLUDE_DIR})
18
19 # Link [Link] and Python libraries
20 target_link_libraries(example PRIVATE
${BOOST_LIBRARY_DIR}/libboost_python311.so ${PYTHON_LIBRARY})
21 target_link_directories(example PRIVATE ${BOOST_LIBRARY_DIR})
22
23 # Set the output name to match the Python module name
24 set_target_properties(example PROPERTIES PREFIX "" SUFFIX ".so")
Ensure the paths to Boost and Python are correct for your setup.
add_library(example SHARED [Link]) creates a shared library named
"example".
target_include_directories and target_link_libraries specify the
necessary include directories and libraries for the build.
3. Build the Library: Create a build directory (e.g., build ), navigate to it, and
run cmake .. followed by make . This will generate the shared library ( .so
file on Linux).
V. Python Code - Steps Involved
This section explains how to use the compiled C++ library within your Python
code.
1. Import the Library: Import the compiled library just like any other Python
module:
1 import example
2. Call the C++ Function: Call the exposed C++ function:
1 # Call the C++ function
2 result = [Link](5, 7)
3 print(f"5 + 7 = {result}")
This completes the process of integrating C++ and Python using Boost-Python.
We've successfully compiled Python and Boost, written a C++ function, exposed
it to Python, and then called it from a Python script. Remember to adjust paths
and Python versions as needed for your specific environment.
Boosting Python on Windows: A Step-by-
Step Guide
This guide outlines the process of boosting Python on Windows using the
[Link] library. We'll cover installing Python, building Boost, and compiling
a C++ example that integrates with Python. All using GCC
1. Installing Python
Before we begin, ensure you have a compatible Python installation on your
Windows system. Download the latest version of Python from the official Python
website. During installation, ensure you select the option to add Python to your
system's PATH environment variable. This allows you to run Python from the
command line without specifying the full path to the Python executable.
2. Building the Boost Library
[Link] requires the Boost C++ Libraries. The following steps detail how to
build Boost on Windows:
Download and Extract: Download the latest version of Boost from the official
Boost website ([Link] Extract the downloaded archive to a
directory of your choice (e.g., C:\Users\YourUser\Desktop\Dev_Env\boost ).
Bootstrap: Open a command prompt or PowerShell window and navigate to
the extracted Boost directory. The bootstrap process on Windows differs
from Linux. Run the following command to bootstrap using the GCC toolset:
1 [Link] gcc
If you prefer to use the Microsoft Visual C++ (MSVC) compiler, simply run:
1 [Link]
Build and Install: After bootstrapping, build and install the Boost libraries. The
following command builds Boost with Python support, using the GCC toolset,
and installs the necessary files to the specified directories:
1 .\b2 toolset=gcc address-model=64 link=shared variant=release --
with-python --
includedir=C:\Users\YourUser\Desktop\Dev_Env\boost\include --
libdir=C:\Users\YourUser\Desktop\Dev_Env\boost\lib -j4 install
Remember to replace C:\Users\YourUser\Desktop\Dev_Env\boost with your
chosen Boost installation directory. The -j4 option specifies using 4 cores for
parallel compilation, which can significantly speed up the build process. Adjust
this number based on your system's capabilities.
3. Compiling C++ Code with CMake
This section demonstrates compiling a C++ example that uses [Link] with
CMake.
CMake Configuration: Create a [Link] file in your project directory.
This file instructs CMake on how to build your project. The following example
demonstrates a basic configuration for a [Link] project:
1 cmake_minimum_required(VERSION 3.12)
2 project(BoostPythonExample)
3
4 # Set the Python version (adjust to match your Python version)
5 set(PYTHON_VERSION 3.11)
6
7 # Specify paths to Boost and Python (adjust these paths)
8 set(BOOST_INCLUDE_DIR
C://Users//YourUser//Desktop//Dev_Env//boost//include//boost-1_87)
9 set(BOOST_LIBRARY_DIR C://Users//YourUser//Desktop//Dev_Env//boost//
10 set(PYTHON_INCLUDE_DIR
C://Users//YourUser//AppData//Local//Programs//Python//Python311//in
11 set(PYTHON_LIBRARY
C://Users//YourUser//AppData//Local//Programs//Python//Python311//py
12
13 # Add the library
14 add_library(example SHARED [Link])
15
16 # Include directories for Boost and Python
17 target_include_directories(example PRIVATE ${BOOST_INCLUDE_DIR}
${PYTHON_INCLUDE_DIR})
18
19 # Link [Link] and Python libraries
20 target_link_libraries(example PRIVATE ${BOOST_LIBRARY_DIR}//libboost_
mgw14-mt-x64-1_87.dll ${PYTHON_LIBRARY})
21 target_link_directories(example PRIVATE ${BOOST_LIBRARY_DIR})
22
23 # Set the output name to a .pyd extension (important for Python modu
24 set_target_properties(example PROPERTIES PREFIX "" SUFFIX ".pyd")
Key Points:
Paths: Ensure the paths to Boost libs and Python are correct for your system.
Python Library: The PYTHON_LIBRARY variable should point to the
[Link] file (where XY is your Python version).
.pyd Extension: Setting the output extension to .pyd is crucial. This creates
a Python extension module that can be imported directly into Python.
This detailed guide provides a comprehensive walkthrough for boosting Python
on Windows using [Link]. Remember to adjust paths and settings
according to your specific environment.
Although I hate windows it has to be done
For using clang attach -pthread flag
1 \b2 toolset=clang address-model=64 link=shared variant=release --
with-python --
includedir=C:\Users\rags\Desktop\Dev_Env\temp\boost\include --
libdir=C:\Users\rags\Desktop\Dev_Env\temp\boost\lib -j4 cxxflags="-
pthread" linkflags="-pthread" install
to compile b2 we still need to pass -pthread but like this
1 set B2_CXXFLAGS=-pthread
2 .\[Link] clang