A CPP library for Python using pybind11
https://pybind11.readthedocs.io/en/stable/basics.html
The pybind11 was included in the project as a submodule using the following command:
git submodule add -b stable https://github.com/pybind/pybind11.git
After cloning the pycpplib repository, run the following command to clone the submodule.
git submodule update --init --recursive
sudo apt install build-essential
sudo apt install python-dev-is-python3
sudo apt install python3-pip
sudo apt install python3-pytest
sudo apt install python3-pybind11
python -m pip install pytest
python -m pip install "pybind11[global]"
Navigate into pybind11 directory and run the following commands
mkdir build
cd build
cmake ..
make check -j 4
Navigate into pybind11 directory and run the following commands
mkdir build
cd build
cmake ..
cmake --build . --config Release --target check
g++ -O3 -Wall -shared -std=c++11 $(python3 -m pybind11 --includes) -fPIC example.cpp -o example.so
The CMakeLists.txt file located at root directory will be used to compile the lib. Maybe you will need to update the pybind11_DIR path in this file to your system.
mkdir build
cd build
cmake .. -G "Visual Studio 17 2022" -A x64
cmake --build . --config Release
After that, the library will be located at
./build/Release/example.lib
python3 testapp.py
I'm still working on this. Still don't know how to load the example.lib file on Windows in a way the Python code remains the same in both systems. The following code works on windows. It can be used for test:
import sys
import os
# Get the path of the parent directory of your script
script_dir = os.path.dirname(__file__)
# Assuming the built module is in `../build/Release`
build_dir = os.path.join(script_dir, "build", "Release")
# Add the build directory to the Python search path
sys.path.append(build_dir)
import example
val = example.add(10, 30)
print(val)
You can modify the testapp.py using the above code and then run the script.