Skip to content

Instantly share code, notes, and snippets.

@ndevenish
Last active March 4, 2024 22:47
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ndevenish/ff771feb6817f7debfa728386110f567 to your computer and use it in GitHub Desktop.
Save ndevenish/ff771feb6817f7debfa728386110f567 to your computer and use it in GitHub Desktop.
Very simple "Getting started" boost-python CMakeLists.txt

Simple Boost-python CMakeLists

Very simple CMakeLists to build a basic python module. CMake is a build-generator; It reads a description of your project and then uses this to generate the actual build system - usually Makefiles, but can also generate e.g. IDE projects. This is why you actually use make to do the build.

Add the CMakeLists.txt to the path with your source, or check out this gist e.g.

$ ls
CMakeLists.txt README.md hello_ext.cpp

Then make a build directory (cmake encourages out-of-source build directories), go into it and run cmake:

$ mkdir _build
$ cd _build
$ cmake ..
...
-- Found PythonLibs: /usr/lib/libpython2.7.dylib (found suitable version "2.7.10", minimum required is "2.7") 
-- Boost version: 1.65.0
-- Found the following Boost libraries:
--   python
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nickd/tmp/bp/_bui

You can then build the library with make:

$ make
Scanning dependencies of target hello_ext
[ 50%] Building CXX object CMakeFiles/hello_ext.dir/hello_ext.cpp.o
[100%] Linking CXX shared module hello_ext.so
[100%] Built target hello_ext

And this should be importable:

$ python
>>> from hello_ext import greet
>>> greet()
'hello, world'

Amending/doing different builds/targets should be relatively intuitive from the CMakeLists.txt.

Implementation Note: Misses out a couple of Cmake best-practices (at time of writing in the middle of a boost/cmake version shear that causes issues with something called 'Imported Targets').

cmake_minimum_required(VERSION 3.5)
# Find python and Boost - both are required dependencies
find_package(PythonLibs 2.7 REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)
# Without this, any build libraries automatically have names "lib{x}.so"
set(CMAKE_SHARED_MODULE_PREFIX "")
# Add a shared module - modules are intended to be imported at runtime.
# - This is where you add the source files
add_library(hello_ext MODULE hello_ext.cpp)
# Set up the libraries and header search paths for this target
target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(hello_ext PRIVATE ${PYTHON_INCLUDE_DIRS})
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
@pangyuteng
Copy link

Great "starter kit"! loving the simplicity. I am using it to troubleshoot installation of boost. Thanks!

@moloned
Copy link

moloned commented Aug 21, 2020

Nice example

One issue I struggled with with in getting this to work is that I had two versions of python installed, python 2.7 and python 3.8.2

While cmake and subsequent make were successful I was getting a runtime error in python or python3 when I attempted to import the greet function

I was able to resolve the issue by removing python2.7

sudo apt remove python2

@pktiuk
Copy link

pktiuk commented Nov 6, 2020

Thank you for this gist it helped me a lot.

@dfaure
Copy link

dfaure commented Nov 27, 2020

For python3, one should obviously change the version number on line 4 of the CMakeLists.txt.
Then it worked for me (after removing CMakeCache.txt), even when having both python2 and python3 installed.

Note: the CMakeLists.txt is missing a project(boost-python-getting-started) on line 2 to fix the CMake warning No project() command is present.

@blogdarkspot
Copy link

It is important remember which the name of the module must be equals the name of the shared library.

@Mjwood51
Copy link

Thank you so much!

@Dariusz1989
Copy link

Thanks for tutorial, I'm hiting wals left and right with it all.. any idea how to configure vcpkg.json to work with that? I keep getting

  Could NOT find Boost (missing: python numpy) (found suitable version "1.81.0", minimum required is "1.81.0")
{
  "name": "hello-world-module",
  "version-string": "1.0.0",
  "builtin-baseline": "223d33be7d3ec6c3d64381ca37f501b8c87dda6a",
  "dependencies": [
    {
      "name": "boost-python",
      "version>=": "1.81.0"
    }
  ]
}

@felixf4xu
Copy link

What will be the cmake install part?

@silviapalara
Copy link

I followed the example and it builds as expected, but when I then go to the dir where the library file was installed and do
python

and
from hello_ext import greet

I get
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'hello_ext'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment