GithubHelp home page GithubHelp logo

snakecuffs's Introduction

help on python c binding

dependencies

install python3.8 from python.org

pip3 install pybind11
pip3 install invoke
pip3 install Cython

security block

mac mojave blocks relative pathed libraries, here is the fix

install_name_tool

def install_name_tool(dependent=None, depends=[]):
    '''
    make the lib full path to get around security blocking relative paths
    '''
    for dependson in depends:
        full_path = os.path.abspath(dependson)
        shell_str='install_name_tool -change {0} {1} {2}'.format(dependson, full_path, dependent)
        print('    install_name_tool: {0}'.format(dependson))
        invoke.run(shell_str)

mac G++ missing symbols

mac compile gets upset about missing symbols

ignore missing symbols on a mac

On Mac OS: the build command is almost the same but it also requires passing the -undefined dynamic_lookup flag so as to ignore missing symbols when building the module:

in task.py

def compile_python_module(cpp_name, extension_name):
    invoke.run(
        "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
        "`python3.8-config --includes` "
        "`python3 -m pybind11 --includes` "
        "-undefined dynamic_lookup "
        "-I . "
        "{0} "
        "-o {1}`python3.8-config --extension-suffix` "
        "-L. -lcppmult -Wl,-rpath,.".format(cpp_name, extension_name)
    )

compile and test

here is the output from a good compile and test run

$ invoke all
==================================================
= Building C Library 
* Complete
==================================================
= Testing ctypes Module 
    In cmult : int: 6 float 2.3 returning  13.8
    In Python: int: 6 float 2.3 return val 48.0

    In cmult : int: 6 float 2.3 returning  13.8
    In Python: int: 6 float 2.3 return val 13.8
==================================================
= Building CFFI Module 
    install_name_tool: libcmult.so
* Complete
==================================================
= Testing CFFI Module 
    In cmult : int: 6 float 2.3 returning  13.8
    In Python: int: 6 float 2.3 return val 13.8
==================================================
= Building C++ Library 
    install_name_tool: libcmult.so
    install_name_tool: cffi_example.cpython-38-darwin.so
* Complete
==================================================
= Building PyBind11 Module 
    install_name_tool: libcmult.so
    install_name_tool: cffi_example.cpython-38-darwin.so
    install_name_tool: libcppmult.so
* Complete
==================================================
= Testing PyBind11 Module 
    In cppmul: int: 6 float 2.3 returning  13.8
    In Python: int: 6 float 2.3 return val 13.8
==================================================
= Building Cython Module 
    install_name_tool: libcmult.so
    install_name_tool: cffi_example.cpython-38-darwin.so
    install_name_tool: libcppmult.so
* Complete
==================================================
= Testing Cython Module 
    In cppmul: int: 6 float 2.3 returning  13.8
    In Python: int: 6 float 2.3 return val 13.8

snakecuffs's People

Contributors

eddo888 avatar

Watchers

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