BindGen-CS is a code generation toolkit for native interop, focused on generating C# bindings from C/C++ headers and generating C bridges from C++ APIs. With proper configurations, this code-generation tool allows you to output a SINGLE-FILE bindings.
This project is a modified derivative of a fork of HexaGen. It has been adapted and extended with a focus on a streamlined, single-file-oriented workflow. Contributions are welcome ⭐. Feel free to open issues or submit pull requests if you encounter problems or have suggestions.
- C# binding generation for C-style native APIs
- C++ to C bridge generation (
BGCS.Cpp2C) - Config-driven generation with JSON config composition (
BaseConfig) - Function-table and direct import modes for generated bindings
- Optional standalone runtime source generation (
Runtime.cs) - Single-file merge auto-removes split generated files
- Configurable naming, filtering, and mapping pipelines
- Runtime support libraries for generated interop code
- .NET SDK 9.0
- Clang/LibClang available in the environment (for parsing C/C++ headers)
- A C/C++ toolchain for validating generated native bridge outputs when needed
BGCS (C/C++ header -> C# bindings):
using BGCS;
var config = CsCodeGeneratorConfig.Load("config.json");
var generator = new CsCodeGenerator(config);
bool ok = generator.Generate("headers/api.h", "Output");Cpp2C (C++ header -> C bridge):
using BGCS.Cpp2C;
var config = Cpp2CGeneratorConfig.Load("config.json");
var generator = new Cpp2CCodeGenerator(config);
generator.Generate("include/demo.hpp", "Output");A basic start up with configuration file:
{
"BaseConfig": {
"Url": "file://config.base.json"
}
"ApiName": "MyApi",
"Namespace": "My.Generated",
"LibName": "mylib",
"ImportType": "DllImport",
"EntryFiles": ["headers/api.h"],
"allowedHeaders": ["headers/api.h"]
}using BGCS;
var cfg = new CsCodeGeneratorConfig("config.json");
var gen = new CsCodeGenerator(cfg);
bool ok = gen.Generate("headers/api.h", "Output");Single-file/runtime behavior summary:
- When
MergeGeneratedFilesToSingleFile = true, BGCS merges output intoSingleFileOutputNameand automatically removes split generated files. - Runtime source generation is explicit:
GenerateRuntimeSource = true: generate standaloneRuntime.csin output root.GenerateRuntimeSource = false: do not generate runtime source.
- Generated bindings always use
using BGCS.Runtime;and runtime namespace remainsBGCS.Runtime. - If your project already references external
BGCS.Runtime, define compile symbolBGCS_RUNTIME_EXTERNALto exclude generated runtime source and avoid duplicate type declarations.
src/BGCS- Main C# binding generatorsrc/BGCS.Cpp2C- C++ to C bridge generatorsrc/BGCS.Runtime- Runtime support for generated bindingssrc/BGCS.CppAst- C/C++ AST parsing layertests/*- Unit/integration/regression test projectsdemo/*- Local demo apps for manual generation validation
BindGen-CS is licensed under the MIT License. See LICENSE.