Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Last active January 15, 2016 09:43
Show Gist options
  • Select an option

  • Save daniel-j-h/7f81fb651e2ba90c15c8 to your computer and use it in GitHub Desktop.

Select an option

Save daniel-j-h/7f81fb651e2ba90c15c8 to your computer and use it in GitHub Desktop.
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2 FATAL_ERROR)
PROJECT(core VERSION 0.0.1 LANGUAGES CXX)
# Sets up some sane compiler flags for C++14 with appropriate warnings;
# tested to work with GCC 4.9+ and Clang 3.6+ --- feel free to modify
IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -Wold-style-cast -Wuninitialized -Wunreachable-code -Wstrict-overflow=3 -D_FORTIFY_SOURCE=2 -ffunction-sections -fdata-sections")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-O1 -Wl,--hash-style=gnu -Wl,--sort-common -Wl,--gc-sections")
IF(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmax-errors=1")
IF (${CMAKE_BUILD_TYPE} MATCHES "Debug")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og -ggdb3 -fno-omit-frame-pointer")
ENDIF()
ELSEIF(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ferror-limit=1")
IF (${CMAKE_BUILD_TYPE} MATCHES "Debug")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
ENDIF()
ENDIF()
ELSE()
MESSAGE(FATAL_ERROR "Unsupported system")
ENDIF()
FIND_PACKAGE(Threads REQUIRED)
FIND_PACKAGE(PythonLibs 2.7 REQUIRED)
FIND_PACKAGE(Boost 1.59.0 REQUIRED COMPONENTS filesystem system program_options thread timer log log_setup unit_test_framework python)
ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK)
ADD_DEFINITIONS(-DBOOST_FILESYSTEM_NO_DEPRECATED)
# Optional: you can make it more modular by putting the following into
# subdir/CMakeLists.txt and ADD_SUBDIRECTORY(subdir) here
# By default Python searches for module.so when doing import module
# force .so on OSX instead of .dylib
IF(APPLE)
SET(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
ENDIF()
ADD_LIBRARY(core SHARED "core.cc")
TARGET_LINK_LIBRARIES(core ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
TARGET_INCLUDE_DIRECTORIES(core SYSTEM PUBLIC ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
# By default CMake prefixes shared libraries with lib; we would get
# libmodule but want module in order to be able to do import module
SET_TARGET_PROPERTIES(core PROPERTIES PREFIX "")
# Optional install target
INSTALL(TARGETS core DESTINATION lib)
#include <boost/python.hpp>
#include <string>
struct Dummy {
std::string s;
void fn() {}
};
BOOST_PYTHON_MODULE(core) {
using namespace boost::python;
class_<Dummy>("Dummy") //
.def_readwrite("str", &Dummy::s) //
.def("fun", &Dummy::fn);
}

Build core.so Python module

mkdir build
pushd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build .

Look, it's there

file core.so

Let's try to use it (make sure to start the interpreter for the Python version you linked against above)

python2
>>> from core import Dummy
>>> x = Dummy()
>>> x.fun()
>>> x.str = "Hello"
>>> print(x.str)
Hello

Cool!

@daniel-j-h
Copy link
Author

Note how requiring CMake 3.0.2, Boost 1.59 and Python 2.7 is somewhat arbitrary (we used those in a project of mine), adapt to your needs.

If you want this to work with Python3 you probably have to not only change the 2.7 to e.g. 3.4 (3 only makes sense for 3.4+ imho), but probably build a local Boost linked against Python 3.

Good luck and be safe out there!

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