Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Forked from ndevenish/CMakeLists.txt
Last active July 12, 2019 21:30
Show Gist options
  • Save pangyuteng/e3232be50022db92984d2886d19df580 to your computer and use it in GitHub Desktop.
Save pangyuteng/e3232be50022db92984d2886d19df580 to your computer and use it in GitHub Desktop.
Very simple "Getting started" boost-python CMakeLists.txt - added Dockerfile

REF

https://gist.github.com/ndevenish/ff771feb6817f7debfa728386110f567 https://gist.github.com/MihaiTheCoder/d1bbe52e81112c6ffe240d74011e786c

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 ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
message(STATUS ${Boost_FOUND})
message(STATUS ${Boost_VERSION})
message(STATUS ${Boost_INCLUDE_DIRS})
message(STATUS ${Boost_LIBRARIES})
message(STATUS ${Boost_SYSTEM_LIBRARY})
message(STATUS ${Boost_FILESYSTEM_LIBRARY})
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y --no-install-recommends \
autotools-dev \
build-essential \
cmake \
git \
gfortran-multilib \
libavcodec-dev \
libavformat-dev \
#libjasper-dev \
libjpeg-dev \
libpng-dev \
liblapacke-dev \
libswscale-dev \
libtiff-dev \
pkg-config \
wget \
zlib1g-dev \
# Protobuf
ca-certificates \
curl \
unzip \
# For Kaldi
python-dev \
automake \
libtool \
autoconf \
subversion \
#For OpenCV
libsm6 libxrender1 libfontconfig1\
# For Kaldi's dependencies
libapr1 libaprutil1 libltdl-dev libltdl7 libserf-1-1 libsigsegv2 libsvn1 m4 \
# For Java Bindings
#openjdk-7-jdk \
# For SWIG
libpcre3-dev && \
rm -rf /var/lib/apt/lists/*
RUN wget https://github.com/Kitware/CMake/releases/download/v3.15.0-rc4/cmake-3.15.0-rc4-Linux-x86_64.sh
RUN bash cmake-3.15.0-rc4-Linux-x86_64.sh --skip-license
ENV BOOST_VERSION 1_60_0
ENV BOOST_DOTTED_VERSION 1.60.0
# Install Boost
RUN wget -q -O - https://sourceforge.net/projects/boost/files/boost/$BOOST_DOTTED_VERSION/boost_$BOOST_VERSION.tar.gz/download | tar -xzf - && \
cd boost_$BOOST_VERSION && \
./bootstrap.sh --prefix=/usr/local/boost-$BOOST_DOTTED_VERSION --with-libraries=python,filesystem,system,test && \
./b2 -d0 -j"$(nproc)" install && \
rm -rf /boost_$BOOST_VERSION
ENV BOOST_ROOT /usr/local/boost-$BOOST_DOTTED_VERSION
ENV BOOST_LIBRARYDIR /usr/local/boost-$BOOST_DOTTED_VERSION/lib
ENV BOOST_INCLUDEDIR /usr/local/boost-$BOOST_DOTTED_VERSION/include/
ENV LD_LIBRARY_PATH $BOOST_LIBRARYDIR:$LD_LIBRARY_PATH
RUN git clone https://gist.github.com/e3232be50022db92984d2886d19df580.git /opt/hello-world-boost1
RUN mkdir -p /opt/hello-world-boost1/_build
WORKDIR opt/hello-world-boost1/_build
RUN cmake ..
RUN make
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment