Coder Social home page Coder Social logo

onixhoque / python2cpp Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 14.8 MB

This repository demonstrates how we can use a C++ library from Python by generating a Cpp shared object

C++ 1.35% Python 0.14% C 0.09% CMake 0.01% Shell 0.03% Makefile 0.02% Cuda 97.46% Jupyter Notebook 0.90% MATLAB 0.01%
python shared-object cpp

python2cpp's Introduction

Accessing C++ Library from Python Code

[New! PyBind11 example added!]

This repo demonstrats how we can use a C++ library from Python by generating a Cpp shared object. This can be done directly by adding a few extra flags in gcc, or using setuptools library provided in Python. The later is a much cleaner approach. We can also use PyBind11, which simplifies the process greatly!

New: Added support for passing function pointer! See performOp() function in mygraph.py

The Cpp library is a simple Graph class that creates adjacency matrix dynamically through constructor. It also has other utility functions for printing the graph, setting edge, and freeing/disposing the dynamic memory.

Compile the shared object using gcc.

  1. Run the following commands to generate shared object from mygraph.cpp file (or just run the make command).
  • g++ -c -fPIC mygraph.cpp -o mygraph.o

  • g++ -shared -Wl,-soname,libmygraph.so -o libmygraph.so mygraph.o

    Note: Conventionally, the generated shared object file's name starts with lib.

  1. Run test.py to check if the graph is being used correctly (or just run the make test command). The mygraph.py works as a wrapper for the cpp library.

Compile the shared object through Python setuptools

  1. Run the following command to generate shared object from mygraph.cpp file (or just run the make command). It will be generated in \build\*\ folder. In this case, the name of the shared object is automatically generated.
  • python3 setup.py build or
  • python setup.py build or
  • py setup.py build
  1. Run test.py to check if the graph is being used correctly (or just run the make test command). The mygraph.py works as a wrapper for the cpp library.

[New!] Compile the shared object through PyBind11 - to support C++11 features

  1. Make sure that you have PyBind11 installed (pip install pybind11).
  2. Run ./make.sh
  3. Run python3 test.py or python test.py

Read the PyBind11 Documentation for more functionalities:

--

Special thanks to the following tutorials

Read More: https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

python2cpp's People

Contributors

onixhoque avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.