blob: a5d074011475dc090d2d5e675fd246c9cd6d3584 [file] [log] [blame]
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001.. _cmake:
2
3Building with CMake
4===================
5
6The following snippet should be a good starting point to create bindings across
7platforms. It assumes that the code is located in a file named :file:`example.cpp`,
8and that the pybind11 repository is located in a subdirectory named :file:`pybind11`.
9
10.. code-block:: cmake
11
12 cmake_minimum_required(VERSION 2.8)
13
14 project(example)
15
16 # Add a CMake parameter for choosing a desired Python version
17 set(EXAMPLE_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example library")
18
19 # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
20 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
21 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
22 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
23 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
24 "MinSizeRel" "RelWithDebInfo")
25 endif()
26 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
27
28 # Try to autodetect Python (can be overridden manually if needed)
29 set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
30 find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} REQUIRED)
31
32 if (UNIX)
33 # Enable C++11 mode
34 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
35
36 # Enable link time optimization and set the default symbol
37 # visibility to hidden (very important to obtain small binaries)
38 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
39 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
40 endif()
41 endif()
42
43 # Include path for Python header files
44 include_directories(${PYTHON_INCLUDE_DIR})
45
46 # Include path for pybind11 header files -- this may need to be changed depending on your setup
47 include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)
48
49 # Create the binding library
50 add_library(example SHARED
51 example.cpp
52 # ... extra files go here ...
53 )
54
55 # Don't add a 'lib' prefix to the shared library
56 set_target_properties(example PROPERTIES PREFIX "")
57
58 if (WIN32)
59 if (MSVC)
60 # Enforce size-based optimization and link time code generation
61 # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
62 # for bigger binding projects due to the limit to 64k addressable sections
63 # /MP enables multithreaded builds (relevant when there are many files).
64 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
65 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
66 endif()
67
68 # .PYD file extension on Windows
69 set_target_properties(example PROPERTIES SUFFIX ".pyd")
70
71 # Link against the Python shared library
72 target_link_libraries(example ${PYTHON_LIBRARY})
73 elseif (UNIX)
74 # It's quite common to have multiple copies of the same Python version
75 # installed on one's system. E.g.: one copy from the OS and another copy
76 # that's statically linked into an application like Blender or Maya.
77 # If we link our plugin library against the OS Python here and import it
78 # into Blender or Maya later on, this will cause segfaults when multiple
79 # conflicting Python instances are active at the same time.
80
81 # Windows is not affected by this issue since it handles DLL imports
82 # differently. The solution for Linux and Mac OS is simple: we just don't
83 # link against the Python library. The resulting shared library will have
84 # missing symbols, but that's perfectly fine -- they will be resolved at
85 # import time.
86
87 # .SO file extension on Linux/Mac OS
88 set_target_properties(example PROPERTIES SUFFIX ".so")
89
90 # Strip unnecessary sections of the binary on Linux/Mac OS
91 if(APPLE)
92 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
93 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
94 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
95 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/example.so)
96 endif()
97 else()
98 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
99 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/example.so)
100 endif()
101 endif()
102 endif()