Skip to content

Instantly share code, notes, and snippets.

@parsa
Forked from tonyseek/README.rst
Created December 21, 2016 00:37
Show Gist options
  • Select an option

  • Save parsa/8c99c2faf64fb80447ef39fe98cd584e to your computer and use it in GitHub Desktop.

Select an option

Save parsa/8c99c2faf64fb80447ef39fe98cd584e to your computer and use it in GitHub Desktop.

Revisions

  1. @tonyseek tonyseek revised this gist Dec 6, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions libhello.cpp
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    #include <iostream>

    class User;

    class User
    {
    std::string name;
  2. @tonyseek tonyseek renamed this gist Dec 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @tonyseek tonyseek revised this gist Dec 6, 2013. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions introduce.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    Run with Python::

    pip-2.7 install cffi
    PYTHON=python2.7 sh go.sh

    Run with PyPy::

    pip-pypy install cffi
    PYTHON=pypy sh go.sh

    Run with Py3K::

    pip-3.3 install cffi
    PYTHON=python3.3 sh go.sh

    The expected output::

    => now run the native one
    hello, world
    => now run the pypy bound one
    hello, cffi
  4. @tonyseek tonyseek created this gist Dec 6, 2013.
    13 changes: 13 additions & 0 deletions go.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #!/usr/bin/env sh

    set -e

    [ -z "$PYTHON" ] && PYTHON="python"

    g++ -o ./hello ./libhello.cpp
    echo "=> now run the native one"
    ./hello

    g++ -o ./libhello.so ./libhello.cpp -fPIC -shared
    echo "=> now run the $PYTHON bound one"
    python hello.py
    14 changes: 14 additions & 0 deletions hello.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    import cffi


    ffi = cffi.FFI()
    ffi.cdef("void cffi_hello(char *name);")
    C = ffi.dlopen("./libhello.so")


    def hello(name):
    C.cffi_hello(name)


    if __name__ == "__main__":
    hello("cffi")
    33 changes: 33 additions & 0 deletions libhello.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #include <iostream>

    class User;

    class User
    {
    std::string name;
    public:
    User(char *name):name(name) {}
    User(std::string &name):name(name) {}

    std::string greet() { return "hello, " + name; }
    };

    void hello(char *name)
    {
    User user(name);
    std::cout << user.greet() << std::endl;
    }

    int main()
    {
    hello((char *) "world");
    return 0;
    }

    extern "C"
    {
    extern void cffi_hello(char *name)
    {
    return hello(name);
    }
    }