Giter VIP home page Giter VIP logo

ctypes-python-wrapper-example's Introduction

A simple example of how to compile a C++ class and make it callable in Python

Create a C++ file called "foo.cpp"

#include

class Foo{ public: void bar(){ std::cout << "Hello" << std::endl; } };

Since ctypes can only talk to C functions, you need to provide those declaring them as extern "C"

extern "C" { Foo* Foo_new(){ return new Foo(); } void Foo_bar(Foo* foo){ foo->bar(); } }

Next you have to compile this to a shared library

g++ -c -fPIC foo.cpp -o foo.o g++ -shared -o libfoo.so foo.o

And finally you have to write your python wrapper (fooWrapper.py)

from ctypes import cdll lib = cdll.LoadLibrary('./libfoo.so')

class Foo(object): def init(self): self.obj = lib.Foo_new()

def bar(self):
    lib.Foo_bar(self.obj)

Once you have that you can call it like

f = Foo() f.bar() #and you will see "Hello" on the screen

Why CTYPES? What about SWIG and BOOST?

The Python standard library includes ctypes, while swig and boost are not part of it. Unlike swig and boost, which depend on extension modules and are thus linked to specific Python minor versions, independent shared objects are not constrained by this limitation. Creating wrappers for swig or boost can be a challenging task, while ctypes does not impose any build requirements.

I tested it using a M1 Mac, the compilation code can be different if you're using a different system

ctypes-python-wrapper-example's People

Contributors

millerbento 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.