blob: 348c54a64d4bc83163a5ff207afb679b3b2ffade [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")
Ivan Smirnov4f88edd2016-01-17 16:42:11 +000018
19 include(CheckCXXCompilerFlag)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020020
21 # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
22 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
23 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
24 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
25 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
26 "MinSizeRel" "RelWithDebInfo")
27 endif()
28 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
29
30 # Try to autodetect Python (can be overridden manually if needed)
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010031 set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
32 if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
33 find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT REQUIRED)
34 else()
35 find_package(PythonLibs REQUIRED)
36 endif()
37
38 # Uncomment the following line if you will also require a matching Python interpreter
39 # find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020040
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010041 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
42 # Enable C++11 mode on C++ / Clang
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020043 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
44
45 # Enable link time optimization and set the default symbol
46 # visibility to hidden (very important to obtain small binaries)
47 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010048 # Default symbol visibility
49 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
50
51 # Check for Link Time Optimization support
52 CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
53 if (HAS_LTO_FLAG)
54 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
55 endif()
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020056 endif()
57 endif()
58
59 # Include path for Python header files
60 include_directories(${PYTHON_INCLUDE_DIR})
61
62 # Include path for pybind11 header files -- this may need to be changed depending on your setup
63 include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)
64
65 # Create the binding library
66 add_library(example SHARED
67 example.cpp
68 # ... extra files go here ...
69 )
70
71 # Don't add a 'lib' prefix to the shared library
72 set_target_properties(example PROPERTIES PREFIX "")
73
74 if (WIN32)
75 if (MSVC)
76 # Enforce size-based optimization and link time code generation
77 # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
78 # for bigger binding projects due to the limit to 64k addressable sections
79 # /MP enables multithreaded builds (relevant when there are many files).
80 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
81 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
82 endif()
83
84 # .PYD file extension on Windows
85 set_target_properties(example PROPERTIES SUFFIX ".pyd")
86
87 # Link against the Python shared library
88 target_link_libraries(example ${PYTHON_LIBRARY})
89 elseif (UNIX)
90 # It's quite common to have multiple copies of the same Python version
91 # installed on one's system. E.g.: one copy from the OS and another copy
92 # that's statically linked into an application like Blender or Maya.
93 # If we link our plugin library against the OS Python here and import it
94 # into Blender or Maya later on, this will cause segfaults when multiple
95 # conflicting Python instances are active at the same time.
96
Wenzel Jakob93296692015-10-13 23:21:54 +020097 # Windows is not affected by this issue since it handles DLL imports
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020098 # differently. The solution for Linux and Mac OS is simple: we just don't
99 # link against the Python library. The resulting shared library will have
100 # missing symbols, but that's perfectly fine -- they will be resolved at
101 # import time.
102
103 # .SO file extension on Linux/Mac OS
104 set_target_properties(example PROPERTIES SUFFIX ".so")
105
106 # Strip unnecessary sections of the binary on Linux/Mac OS
107 if(APPLE)
108 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
109 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
110 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
111 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/example.so)
112 endif()
113 else()
114 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
115 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/example.so)
116 endif()
117 endif()
118 endif()