ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pybind11 with cmake building

2021-06-29 13:01:43  阅读:294  来源: 互联网

标签:building set cmake sampling add pybind11


Below is a CMakeLists.txt which is used for building the source code “sampling.cpp”, during which pybind11 helps to output a shared library “sampling.so” that can be imported by python.

cmake_minimum_required(VERSION 2.8.12)
project(sampling)
set(CMAKE_CXX_STANDARD 11)

set(pybind11_DIR /home/something/miniconda3/envs/TestEnv/lib/python3.6/site-packages/pybind11/share/cmake/pybind11/)

find_package(pybind11 REQUIRED)     # add_subdirectory(pybind11)
pybind11_add_module(sampling sampling.cpp)



# add 2021/6/29
# For building with cmake, to make 'find_package' work, there are several ways: 
# 1. In the virtual env, use: pip install "pybind11[global]", instead of: pip install pybind11, then there is no need to set pybind11_DIR;
# 2. Or, add the 5th line code to set pybind11_DIR which includes the configuration file "pybind11Config.cmake" (or "pybind11-config.cmake");
# 3. Add the installation prefix of "pybind11" to CMAKE_PREFIX_PATH  --- how ?  -> by replacing [cmake .. ] with [cmake -DCMAKE_PREFIX_PATH=/home/xiongkai/miniconda3/envs/s2cnn/lib/python3.6/site-packages/pybind11/ .. ]
# 4. By providing "Findpybind11.cmake" in CMAKE_MODULE_PATH          --- how ?

# Or, building manually:
# c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) sampling.cpp -o sampling$(python3-config --extension-suffix)

# Interesting, the output .so by above two building methods have different size.
# Specifically, 144 kb for building manually, and 587 kb for cmake building.

# one more note
# pybind11_add_module() is identical to:
# add_library(sampling MODULE sampling.cc)                  # if no MODULE, then outputs a static '.a' library
# target_link_libraries(sampling PRIVATE pybind11::module)  # the output '.so' file is of size 798 kb. It is not a high quality generation. 

标签:building,set,cmake,sampling,add,pybind11
来源: https://blog.csdn.net/Bear_Kai/article/details/118332698

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有