{"id":17062590,"url":"https://github.com/wardbrian/ffi-examples","last_synced_at":"2026-05-18T19:04:47.356Z","repository":{"id":201682454,"uuid":"695196655","full_name":"WardBrian/ffi-examples","owner":"WardBrian","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-19T19:02:10.000Z","size":46,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-28T15:23:33.252Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WardBrian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-22T15:06:48.000Z","updated_at":"2023-11-17T20:07:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"2de7ae07-dd75-4cbe-aad2-f008ff9c5b64","html_url":"https://github.com/WardBrian/ffi-examples","commit_stats":null,"previous_names":["wardbrian/ffi-examples"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WardBrian%2Fffi-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WardBrian%2Fffi-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WardBrian%2Fffi-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WardBrian%2Fffi-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WardBrian","download_url":"https://codeload.github.com/WardBrian/ffi-examples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245078150,"owners_count":20557279,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T10:50:18.040Z","updated_at":"2025-10-06T23:40:16.352Z","avatar_url":"https://github.com/WardBrian.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FFI Examples\n\nThis repo contains the slides and examples for my talk at the [2023 Flatiron Wide Annual Meeting (FWAM)](https://www.simonsfoundation.org/event/flatiron-wide-autumn-meeting/).\n\n## Requirements\n\nA C and C++ compiler, `make`, python with the pybind11 and numpy packages.\n\nThe [slides](./slides.md) are best viewed with [slides](https://github.com/maaslalani/slides).\n\n## Outline\n\nI will be focusing on the case of calling compiled code from a\nhigher-level language. This is because it is the more common (and useful) case,\nand because it is the case which is the most similar between pairs of languages.\n\n\n- Basic concepts\n  - Linking and namespaces, `nm`\n  - Name mangling and `extern \"C\"`\n    - I am not familiar with Fortran but my understanding is\n      you can use `iso_c_binding` to 'pretend' you're a C library.\n\n- Basic FFI\n  - Shared libraries, manual lookup\n    - Calling conventions\n    - I will be using Python, but this style is most\n      useful if you are providing bindings for multiple\n      languages.\n      This style works in Python, Julia, R[^1], MATLAB, and likely more.\n    - `ctypes`\n\n[^1]: R has some extra weird limitations, but they're all things that you can work around if needed.\n\n- Language-specific options (`Python.h`, `pybind11`, etc.)\n  - Can be more powerful or convenient, but really only work for one language at a time.\n\n- Common concerns[^2]\n  - Error handling\n  - Cross-language memory management\n  - Converting between idioms (e.g. out parameters)\n\n[^2]: Note that these are also concerns for API design generally, not just FFI.\n\n- General tools that do it all for you (SWIG, etc.)\n  - Can be powerful!\n  - Can also be a pain and generate a _lot_ of code.\n  - Personally, happy medium is things like clang2py which automate boilerplate of the \"manual\" approach\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwardbrian%2Fffi-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwardbrian%2Fffi-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwardbrian%2Fffi-examples/lists"}