{"id":40669057,"url":"https://github.com/csiro-hydroinformatics/c-api-wrapper-generation","last_synced_at":"2026-01-21T09:17:56.293Z","repository":{"id":30472420,"uuid":"34026446","full_name":"csiro-hydroinformatics/c-api-wrapper-generation","owner":"csiro-hydroinformatics","description":"Generate language bindings around an existing C/C++ API","archived":false,"fork":false,"pushed_at":"2025-08-22T03:45:37.000Z","size":280,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-10T17:04:44.765Z","etag":null,"topics":["api-wrapper","cffi","codegen","interoperability","pinvoke","rcpp"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csiro-hydroinformatics.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":"2015-04-16T00:53:37.000Z","updated_at":"2023-03-15T17:08:30.000Z","dependencies_parsed_at":"2023-02-11T23:40:28.391Z","dependency_job_id":"348af056-f602-4bf9-be9f-37f96e00f232","html_url":"https://github.com/csiro-hydroinformatics/c-api-wrapper-generation","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/csiro-hydroinformatics/c-api-wrapper-generation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csiro-hydroinformatics%2Fc-api-wrapper-generation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csiro-hydroinformatics%2Fc-api-wrapper-generation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csiro-hydroinformatics%2Fc-api-wrapper-generation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csiro-hydroinformatics%2Fc-api-wrapper-generation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csiro-hydroinformatics","download_url":"https://codeload.github.com/csiro-hydroinformatics/c-api-wrapper-generation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csiro-hydroinformatics%2Fc-api-wrapper-generation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28630946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T04:47:28.174Z","status":"ssl_error","status_checked_at":"2026-01-21T04:47:22.943Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["api-wrapper","cffi","codegen","interoperability","pinvoke","rcpp"],"created_at":"2026-01-21T09:17:56.214Z","updated_at":"2026-01-21T09:17:56.275Z","avatar_url":"https://github.com/csiro-hydroinformatics.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Generate bindings for a C API\n\n[![license](http://img.shields.io/badge/license-GPLv3-red.svg)](https://raw.githubusercontent.com/csiro-hydroinformatics/c-api-wrapper-generation/master/LICENSE)\n![status](https://img.shields.io/badge/status-beta-orange.svg)\n[![DOI](https://zenodo.org/badge/34026446.svg)](https://zenodo.org/badge/latestdoi/34026446)\n\nFrom a C API with functions such as:\n\n```c++\nSWIFT_API HYPERCUBE_PTR CreateHypercubeParameterizer(const char* strategy);\n```\n\nGenerate the glue code to surface it to R, Python, Matlab, .NET, etc.\n\n## Background\n\nAround 2014 I needed in a project to (re)generate bindings for C++ scientific research modelling code _via a C API_ for at least R, Python, Matlab and C#. A manual approach was not a sustainable option. I first tried to apply or adapt a few of the many third party options (incl. some heavyweights in the wrapping field such as SWIG). I ended up with more difficulties than I bargained for, and despite my reluctance enacted plan B, a custom solution.\n\nIt may, or may not, suit your needs. It is used on an ongoing basis for quite large APIs with hundreds of functions. I hope it can help alleviate your language interop glue code maintenance.\n\n## Overview\n\nA typical C API, build on top of C++ for a better cross-language interoperability, often looks like this [hydrologic modelling](https://www.mssanz.org.au/modsim2015/L15/perraud.pdf) example:\n\n```c++\nSWIFT_API HYPERCUBE_PTR CreateHypercubeParameterizer(const char* strategy);\nSWIFT_API void AddParameterDefinition(HYPERCUBE_PTR hypercubeParameterizer, const char* variableName, double min, double max, double value);\nSWIFT_API double GetParameterValue(HYPERCUBE_PTR hypercubeParameterizer, const char* variableName);\nSWIFT_API void DisposeSharedPointer(VOID_PTR_PROVIDER_PTR ptr);\n```\n\nwith C macro expanded:\n\n```c++\nextern void* CreateHypercubeParameterizer(const char* strategy);\nextern void AddParameterDefinition(void* hypercubeParameterizer, const char* variableName, double min, double max, double value);\nextern double GetParameterValue(void* hypercubeParameterizer, const char* variableName);\nextern void DisposeSharedPointer(void* ptr);\n```\n\nThis present code generation tool was created to generate bindings around native libraries with a C API.\n\nSay you want to surface this API in R using [Rcpp](http://www.rcpp.org/). There are plenty of design options of course, but chances are you will need some boilerplate `C++` code with Rcpp classes (`XPtr`, `CharacterVector`, etc.) looking like:\n\n```c++\n// [[Rcpp::export]]\nXPtr\u003copaque_pointer_handle\u003e CreateHypercubeParameterizer_Rcpp(CharacterVector strategy)\n{\n    auto result = CreateHypercubeParameterizer(strategy[0]);\n    auto x = XPtr\u003copaque_pointer_handle\u003e(new opaque_pointer_handle(result));\n    return x;\n}\n```\n\nand boilerplate `R` code such as:\n\n```R\n#' CreateHypercubeParameterizer_R\n#'\n#' CreateHypercubeParameterizer_R Wrapper function for CreateHypercubeParameterizer\n#'\n#' @param strategy R type equivalent for C++ type const char*\n#' @export\nCreateHypercubeParameterizer_R \u003c- function(strategy) {\n  strategy \u003c- cinterop::getExternalXptr(strategy)\n  result \u003c- CreateHypercubeParameterizer_Rcpp(strategy)\n  return(cinterop::mkExternalObjRef(result, 'HYPERCUBE_PTR'))\n}\n```\n\n`opaque_pointer_handle` and `mkExternalObjRef` are managing native object lifetime. They are not included in the present repository, but if useful to you in the related [cpp interop commons](https://github.com/csiro-hydroinformatics/c-interop) repository.\n\n## Getting started\n\n### Windows\n\nFrom the development command line prompt of visual studio (or a compiler toolchain):\n\n```bat\nMSBuild c:\\src\\c-api-wrapper-generation\\ApiWrapperGenerator\\ApiWrapperGenerator.sln /t:Build /p:Configuration=Debug\n```\n\n### Linux\n\n#### prerequisites\n\nCheck dotnet is available with `which dotnet`, and `dotnet --info`.\n\nAs of April 2022, dotnet can be installed on Linux Debian using debian package repositories managed by Microsoft. This is the prefered way, and seems to work fine for generating code from F# scripts\n\n_Possibly deprecated_: if need be follow the [Manual Install](https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian#manual-install). There are other ways to obtain dotnet.\n\n`cd ~/Downloads` `ls dotnet*`\n\n```text\ndotnet-sdk-5.0.101-linux-x64.tar.gz\ndotnet-sdk-3.1.404-linux-x64.tar.gz\ndotnet-sdk-2.1.811-linux-x64.tar.gz\ndotnet-sdk-3.1.402-linux-x64.tar.gz\n```\n\n```bash\ntar zxf dotnet-sdk-2.1.811-linux-x64.tar.gz  -C \"$HOME/dotnet\"\n```\n\nOptionally if you want newer versions:\n\n```bash\ntar zxf dotnet-sdk-3.1.404-linux-x64.tar.gz  -C \"$HOME/dotnet\"\ntar zxf dotnet-sdk-5.0.101-linux-x64.tar.gz  -C \"$HOME/dotnet\"\n```\n\n`nano ~/.bashrc` then adding\n\n```sh\nexport DOTNET_ROOT=$HOME/dotnet\nexport PATH=$PATH:$HOME/dotnet\n```\n\n#### prerequisites for capigen\n\nIf you want to generate code from the R package `capigen`, you need rClr. This may be deprecated as of April 2022.\n\n```sh\nsudo apt install mono-xbuild\nsudo apt install libmono-2.0-dev\nsudo apt install msbuild\n\ncd ~/src/github_jm/rClr\n\ncd ~/src/github_jm\nexport BUILDTYPE=Debug\nR CMD INSTALL --no-test-load rClr\n\ncd ~/src/github_jm\nR CMD build --no-build-vignettes rClr\nR CMD INSTALL rClr_0.9.0.tar.gz\n```\n\n#### Building our codegen engine\n\n```sh\n# cd ~/src/github_jm/c-api-wrapper-generation\ncd engine/ApiWrapperGenerator\ndotnet restore ApiWrapperGenerator.sln\n```\n\n```bash\ndotnet build --configuration Release --no-restore ApiWrapperGenerator.sln\ndotnet build --configuration Debug --no-restore ApiWrapperGenerator.sln\n\ncd ../TestApiWrapperGenerator/\ndotnet test TestApiWrapperGenerator.csproj \n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsiro-hydroinformatics%2Fc-api-wrapper-generation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsiro-hydroinformatics%2Fc-api-wrapper-generation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsiro-hydroinformatics%2Fc-api-wrapper-generation/lists"}