Nico Schlömer
Nico Schlömer

Reputation: 58891

pybind11, cmake: how to install files?

I'm interfacing a C++ library with the ever more popular pybind11 to get native Python bindings; configuration is via CMake.

My CMakeLists.txt looks like

cmake_minimum_required(VERSION 3.0)

project(foo)

FILE(GLOB foo_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")

FIND_PACKAGE(pybind11 REQUIRED)
pybind11_add_module(mylib ${foo_SRCS})

This doesn't seem to register installation rules, however. Hence, while everything works as expected in the build tree, make install doesn't do anything.

What needs to be added to get the installation in order?

Upvotes: 5

Views: 4987

Answers (2)

saell
saell

Reputation: 11

I installed the created library to the python<version>/site-packages directory

find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
...
install(TARGETS mylib
        COMPONENT python
        LIBRARY DESTINATION "lib/python${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}/site-packages"
        ARCHIVE DESTINATION "lib"
        RUNTIME DESTINATION "bin")

That way the python interpreter finds the library without additional manipulation of PYTHONPATH

The variables Python3_VERSION_MAJOR and Python3_VERSION_MINOR are provided by the `find_package' instruction

Upvotes: 0

utopia
utopia

Reputation: 1535

Just the usual CMake install commands:

include(GNUInstallDirs)

install(TARGETS mylib
  COMPONENT python
  RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")

for Unix. You can manually make up the destination directories for other platforms. The "COMPONENT" part is optional, but just neater for different types of installers.

Upvotes: 3

Related Questions