caiorss / example-pybind11-vcpkg

Example project of a C++ native code Python3 module with Pybind11 managed by CMake and vcpkg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python3 native module - written in C++ with Pybind11.

This example project contains a Python3 native module written in C++ with the help of CMake and Pybind11, vcpkg (C++) Package manager.

Compile:

  • 1 - Install CMake and Python 3 devel (Example for Fedora Linux.)
$ dnf install cmake 
$ dnf install python3-devel 
  • 2 - Install vpckg package manager.
$ mkdir -p ~/build && cd ~/build
$ git clone https://github.com/Microsoft/vcpkg
$ cd vcpkg/
$ sh bootstrap-vcpkg.sh 
$ ./vcpkg 
  • 3 - Install pybind11 package:
$ cd ~/build
$ ./vcpkg install pybind11

Note: On Windows use the following command to install the library for 64 bits target:

$ vcpkg install --triplet x64-windows pybind11l 
  • 4 - Build the Python module.

Building on Linux:

$ make install
cmake -B./cache -H. \
        -DCMAKE_TOOLCHAIN_FILE=/home/archbox/build/vcpkg//scripts/buildsystems/vcpkg.cmake
-- The C compiler identification is GNU 8.2.1
-- The CXX compiler identification is GNU 8.2.1
-- Check for working C compiler: /usr/lib64/ccache/cc
-- Check for working C compiler: /usr/lib64/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done

 ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ... 

cmake --build ./cache --target install
gmake[1]: Entering directory '/home/archbox/root-scripts/vcpkg-test1/cache'
gmake[2]: Entering directory '/home/archbox/root-scripts/vcpkg-test1/cache'

 ... ... ... 

[100%] Built target cashflow
gmake[2]: Leaving directory '/home/archbox/root-scripts/vcpkg-test1/cache'
Install the project...
-- Install configuration: ""
-- Installing: /home/archbox/root-scripts/vcpkg-test1/cashflow.cpython-36m-x86_64-linux-gnu.so
gmake[1]: Leaving directory '/home/archbox/root-scripts/vcpkg-test1/cache'

Building on Windows:

$ cmake -H. -Bbuild -G "Visual Studio 15 2017 Win64"
$ cmake --build build --target install

# Check produced files
$ ls cashflow.cp36-win_amd64.pyd
cashflow.cp36-win_amd64.pyd*

# Check file type => It is a DLL. 
$ "C:\Program Files\Git\usr\bin\file.exe" cashflow.cp36-win_amd64.pyd
cashflow.cp36-win_amd64.pyd: PE32+ executable (DLL) (console) x86-64, for MS Windows

$ "C:\Program Files\Git\usr\bin\du.exe" -h cashflow.cp36-win_amd64.pyd
884K    cashflow.cp36-win_amd64.pyd
  • 5 - Test the native code Python module: cashflow.
# Check native module 
$ file cashflow.cpython-36m-x86_64-linux-gnu.so

cashflow.cpython-36m-x86_64-linux-gnu.so: ELF 64-bit LSB shared
object, x86-64, version 1 (SYSV), dynamically linked,
BuildID[sha1]=49febf0b87ef203341829d35c5189406bdcb7132, not stripped

# Test module 
$ python3 client1.py 
Testing cash flow class
Before adding 200
i  = 0 c  =    -100.00
i  = 1 c  =      10.00
i  = 2 c  =      20.00
i  = 3 c  =      30.00
i  = 4 c  =      15.00
 Net present value => NPV(c, 8%) = -38.75354931215318
After adding 200
i  = 0 c  =    -100.00
i  = 1 c  =      10.00
i  = 2 c  =      20.00
i  = 3 c  =      30.00
i  = 4 c  =      15.00
i  = 5 c  =     200.00
 Net present value => NPV(c, 8%) = 97.36309009459742

Pybind11 Documentation:

About

Example project of a C++ native code Python3 module with Pybind11 managed by CMake and vcpkg


Languages

Language:C++ 48.7%Language:CMake 28.6%Language:Makefile 13.2%Language:Python 9.5%