Skip to content

vipontes/pycpplib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pycpplib

A CPP library for Python using pybind11

Reference

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

Linux Dependencies

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

Windows Dependencies

python -m pip install pytest
python -m pip install "pybind11[global]"

Generating the library on Linux

Navigate into pybind11 directory and run the following commands

mkdir build
cd build
cmake ..
make check -j 4

Generating the library on Windows

Navigate into pybind11 directory and run the following commands

mkdir build
cd build
cmake ..
cmake --build . --config Release --target check

CPP compile command on Linux:

g++ -O3 -Wall -shared -std=c++11 $(python3 -m pybind11 --includes) -fPIC example.cpp -o example.so

CPP compile command on Windows:

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

Testing the extension in a Python script (LINUX):

python3 testapp.py

Testing the extension in a Python script (Windows):

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.

About

A CPP library for Python using pybind11

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors