docker run -it -p 8888:8888 –privileged -v /root/docker/kuyoh_vcpkg/2023.12.12-ubuntu22.04/opt:/opt -v /root:/root -v /home/qgb:/home/qgb docker.io/kuyoh/vcpkg:2023.12.12-ubuntu22.04
export http_proxy=socks5h://192.168.1.20:41080;export https_proxy=$http_proxy # 不要这句也可以vcpkg install numcpp 5.8s
vcpkg install pybind11
cd home/qgb/github/example-pybind11-vcpkg
cmake -DCMAKE_TOOLCHAIN_FILE:STRING=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Release -D PYTHON_EXECUTABLE=/home/qgb/anaconda3/bin/python -H. -B./cache -G Ninja
cmake –build ./cache –config Release –target all -j 32 –
root@f92f2e6a26ce:/home/qgb/github/example-pybind11-vcpkg# make run cmake --build ./cache --target install [0/1] Install the project... -- Install configuration: "Release" -- Up-to-date: /home/qgb/github/example-pybind11-vcpkg/cashflow.cpython-311-x86_64-linux-gnu.so /home/qgb/anaconda3/bin/python 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
apt install -y aria2 zip unzip autoconf automake autoconf-archive vcpkg install boost python3:x64-linux # 出错执行上面apt
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: