日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pybind11 的 CMakeList 说明

發布時間:2023/12/31 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pybind11 的 CMakeList 说明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文以 Pybind 11 的CMakeList 作為例子 進行說明 CMakeList的寫法

pybind11 簡介

pybind11 是一個輕量級的只包含一組頭文件的 C++ 庫,可以在 Python 中使用 C++ 類型。主要用于創建已有 C++ 代碼的 Python 封裝版本

Github 上 Pybind11 工程代碼位置:
pybind11
CMakeList

pybind11 的CMakeList詳解

  • CMakeList 最小版本要求
  • cmake_minimum_required(VERSION 3.4) if(${CMAKE_VERSION} VERSION_LESS 3.18)cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) else()cmake_policy(VERSION 3.18) endif() 2. 從Pybind11 的源碼中提取版本 # Extract project version from source file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h"pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")foreach(ver ${pybind11_version_defines})if(ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]])set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}")endif() endforeach()if(PYBIND11_VERSION_PATCH MATCHES [[([a-zA-Z]+)]])set(pybind11_VERSION_TYPE "${CMAKE_MATCH_1}") endif() string(REGEX MATCH "[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}")
  • 定義項目名稱為pybind11, 語言為CXX, 版本號
  • project(pybind11LANGUAGES CXXVERSION "${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}")
  • 包含引用的三方庫的頭文件,如 GNUInstallDirs, CMakePackageConfigHelpers CMakeDependentOption
  • include(GNUInstallDirs) include(CMakePackageConfigHelpers) include(CMakeDependentOption)if(NOT pybind11_FIND_QUIETLY) message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}") endif()
  • pybind11 與操作系統相關的一些設置
  • # Check if pybind11 is being used directly or via add_subdirectory if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)### Warn if not an out-of-source buildsif(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)set(lines"You are building in-place. If that is not what you intended to ""do, you can clean the source directory with:\n""rm -r CMakeCache.txt CMakeFiles/ cmake_uninstall.cmake pybind11Config.cmake ""pybind11ConfigVersion.cmake tests/CMakeFiles/\n")message(AUTHOR_WARNING ${lines})endif()set(PYBIND11_MASTER_PROJECT ON)if(OSX AND CMAKE_VERSION VERSION_LESS 3.7)# Bug in macOS CMake < 3.7 is unable to download catchmessage(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended")elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8)# Only tested with 3.8+ in CI.message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested")endif()message(STATUS "CMake ${CMAKE_VERSION}")if(CMAKE_CXX_STANDARD)set(CMAKE_CXX_EXTENSIONS OFF)set(CMAKE_CXX_STANDARD_REQUIRED ON)endif() else()set(PYBIND11_MASTER_PROJECT OFF)set(pybind11_system SYSTEM) endif()
  • pybind11 查找 依賴項,比如 Python
  • # Options option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_NOPYTHON "Disable search for Python" OFF)cmake_dependent_option(USE_PYTHON_INCLUDE_DIR"Install pybind11 headers in Python include directory instead of default installation prefix"OFF "PYBIND11_INSTALL" OFF)cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF"NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)
  • pybind11 設置 需要編譯的 include 文件
  • # NB: when adding a header don't forget to also add it to setup.py set(PYBIND11_HEADERSinclude/pybind11/detail/class.hinclude/pybind11/detail/common.hinclude/pybind11/detail/descr.hinclude/pybind11/detail/init.hinclude/pybind11/detail/internals.hinclude/pybind11/detail/typeid.hinclude/pybind11/attr.hinclude/pybind11/buffer_info.hinclude/pybind11/cast.hinclude/pybind11/chrono.hinclude/pybind11/common.hinclude/pybind11/complex.hinclude/pybind11/options.hinclude/pybind11/eigen.hinclude/pybind11/embed.hinclude/pybind11/eval.hinclude/pybind11/iostream.hinclude/pybind11/functional.hinclude/pybind11/numpy.hinclude/pybind11/operators.hinclude/pybind11/pybind11.hinclude/pybind11/pytypes.hinclude/pybind11/stl.hinclude/pybind11/stl_bind.h)
  • pybind11 設置 需要編譯的 include 文件 - 定義 Cache variables 的變量
  • string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS"${PYBIND11_HEADERS}")# Cache variables so pybind11_add_module can be used in parent projects set(PYBIND11_INCLUDE_DIR"${CMAKE_CURRENT_LISCACHE INTERNAL "")
  • pybind11 設置 需要編譯的 include 文件
  • add_library(pybind11_headers INTERFACE) add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target add_library(pybind11::headers ALIAS pybind11_headers) # easier to use/rememberinclude("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake")# Relative directory setting if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS}) elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) endif()
  • 設置Install的一些參數,如install 的路經
  • target_include_directories(pybind11_headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)target_compile_features(pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literalscxx_right_angle_brackets)if(PYBIND11_INSTALL)install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})# GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".set(PYBIND11_CMAKECONFIG_INSTALL_DIR"share/cmake/${PROJECT_NAME}"CACHE STRING "install path for pybind11Config.cmake")configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})if(CMAKE_VERSION VERSION_LESS 3.14)# Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does# not depend on architecture specific settings or libraries.set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})unset(CMAKE_SIZEOF_VOID_P)write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmakeVERSION ${PROJECT_VERSION}COMPATIBILITY AnyNewerVersion)set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P})else()# CMake 3.14+ natively supports header-only librarieswrite_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmakeVERSION ${PROJECT_VERSION}COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)endif()install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmaketools/FindPythonLibsNew.cmaketools/pybind11Common.cmaketools/pybind11Tools.cmaketools/pybind11NewTools.cmakeDESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})if(NOT PYBIND11_EXPORT_NAME)set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")endif()install(TARGETS pybind11_headers EXPORT "${PYBIND11_EXPORT_NAME}")install(EXPORT "${PYBIND11_EXPORT_NAME}"NAMESPACE "pybind11::"DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})# Uninstall targetif(PYBIND11_MASTER_PROJECT)configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in""${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)endif() endif()
  • 包含 Test 目錄
  • # BUILD_TESTING takes priority, but only if this is the master project if(PYBIND11_MASTER_PROJECT AND DEFINED BUILD_TESTING)if(BUILD_TESTING)if(_pybind11_nopython)message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")else()add_subdirectory(tests)endif()endif() else()if(PYBIND11_TEST)if(_pybind11_nopython)message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")else()add_subdirectory(tests)endif()endif() endif()

    至此,Pybind11 的CMakeList 才結束。

    總結

    以上是生活随笔為你收集整理的Pybind11 的 CMakeList 说明的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。