HexaGen is a comprehensive code generation toolkit for C# and C++ projects. It uses HexaGen.CppAst to parse C/C++ headers and automatically generates C# bindings and wrappers. HexaGen simplifies the process of integrating native libraries with C# applications by automating the creation of interop code.
- C# Bindings for C Libraries: Automatically generate C# bindings from C headers with support for functions, structs, enums, and callbacks
- C# Bindings for COM Libraries: Create C# bindings for COM interfaces and objects
- C++ to C Wrappers: Generate C wrappers around C++ libraries to facilitate C interop
- Flexible Configuration: JSON-based configuration system with inheritance and composition support
- Advanced Function Generation: Multiple parameter handling strategies including spans, refs, delegates, and default values
- Type Mapping: Configurable type mappings and conversions
- Extension Methods: Generate extension methods for improved API ergonomics
- Constants to Enums: Convert C preprocessor constants to strongly-typed C# enums
- HexaGen - Main code generation tool and CLI
- HexaGen.Core - Core functionality and utilities for code generation
- HexaGen.Cpp2C - C++ to C wrapper generator
- HexaGen.Runtime - Runtime support library for generated code (multi-target: .NET 9/8/7/6, .NET Standard 2.0/2.1, .NET Framework 4.7.2, Android)
- HexaGen.Runtime.COM - Runtime support for COM interop (multi-target: .NET 9/8/7/6, .NET Standard 2.0/2.1, .NET Framework 4.7.2)
- HexaGen.Language - Language parsing and processing utilities
- HexaGen.Tests - Unit tests
- HexaGen.PerformanceTests - Performance benchmarks
- .NET SDK 9.0 (or compatible version)
- Clang 17.0.4 or later (for parsing C/C++ headers)
- Visual Studio 2022 or later (recommended for development)
Install HexaGen via NuGet Package Manager:
dotnet add package HexaGen-
Clone the Repository:
git clone https://github.com/HexaEngine/HexaGen.git cd HexaGen -
Build the Project:
dotnet build
Create a configuration file (e.g., config.json):
{
"ApiName": "MyLibrary",
"Namespace": "MyLibrary.Generated",
"ImportType": "DllImport",
"GenerateExtensions": true
}Generate C# bindings programmatically:
using HexaGen;
var config = CsCodeGeneratorConfig.Load("config.json");
var generator = new CsCodeGenerator(config);
generator.Generate("mylibrary.h", "Output");HexaGen uses a JSON-based configuration system that supports:
- BaseConfig: Configuration inheritance from other files
- Type Mappings: Custom type conversions
- Function Rules: Advanced parameter transformation rules
- Constants to Enum: Convert preprocessor defines to enums
- Import Types: DllImport, LibraryImport, or delegates
Example configuration with inheritance:
{
"BaseConfig": {
"Url": "file://config.base.json",
"IgnoredProperties": ["IgnoredTypes"]
},
"ApiName": "MyAPI",
"Namespace": "MyAPI.Generated",
"ImportType": "LibraryImport"
}For generating C wrappers around C++ libraries:
using HexaGen.Cpp2C;
var config = Cpp2CGeneratorConfig.Load("cpp2c-config.json");
var generator = new Cpp2CGenerator(config);
generator.Generate("mylibrary.hpp", "Output");Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes following the project's coding standards
- Add appropriate tests
- Submit a pull request
HexaGen is licensed under the MIT License. See the LICENSE.txt file for details.
- GitHub Repository
- NuGet Package
- HexaGen.CppAst - The underlying C++ parser (custom fork)