This collection of Julia scripts automatically generates C header files from Julia functions marked with the @ccallable macro. This is useful when creating Julia libraries that need to be called from C/C++ code.
The main generator script that provides a simple and robust approach to generating C headers. It uses a manual definition approach where you explicitly define function signatures, avoiding complex AST parsing.
Features:
- Direct type mapping from Julia to C types
- Support for pointers, arrays, and basic types
- Generates complete C headers with guards and C++ compatibility
- Includes a convenient
@ccfuncmacro for defining signatures
The original generator with comprehensive type mappings. This script provides the core functionality for converting Julia types to their C equivalents.
Features:
- Extensive Julia-to-C type mapping dictionary
- Support for standard C types (int, float, pointers, etc.)
- Handles arrays as opaque pointers
- Generates C++ compatible headers
Parser for extracting @ccallable macro signatures from Julia source code. This script can analyze Julia files and extract function information automatically.
Features:
- Parses Julia AST to find
@ccallablefunctions - Extracts function names, return types, and arguments
- Supports both file and string parsing
- Handles various
@ccallablesyntax forms
Complete example demonstrating the functionality of the header generator with a sample Julia library.
Demonstrates:
- Various
@ccallablefunction patterns - Mathematical operations
- Array and string handling
- Error handling patterns
- Complex number operations
include("ccallable_header_generator.jl")
# Define your functions
functions = [
CCFunction("add_numbers", "Cint", [("a", "Cint"), ("b", "Cint")]),
CCFunction("process_array", "Cvoid", [("data", "Ptr{Cdouble}"), ("size", "Csize_t")]),
CCFunction("get_version", "Cstring", []),
]
# Generate header
generate_c_header(
functions,
"mylib.h",
header_guard="MYLIB_H",
includes=["<string.h>"],
comment="C API for My Julia Library"
)include("parse_ccallable.jl")
# Parse Julia source file
functions = extract_ccallable_from_file("mylib.jl")
# Or parse from string
julia_code = """
@ccallable function compute_sum(x::Cint, y::Cint)::Cint
return x + y
end
"""
functions = extract_ccallable_from_string(julia_code)
# Generate header
generate_header_file(functions, "mylib.h", header_guard="MYLIB_H")The scripts support the following Julia to C type mappings:
| Julia Type | C Type |
|---|---|
Int8 |
int8_t |
UInt8 |
uint8_t |
Int16 |
int16_t |
UInt16 |
uint16_t |
Int32 |
int32_t |
UInt32 |
uint32_t |
Int64 |
int64_t |
UInt64 |
uint64_t |
Float32 |
float |
Float64 |
double |
Bool |
bool |
Cint |
int |
Cdouble |
double |
Cstring |
const char* |
Ptr{T} |
T* |
Cvoid |
void |
Nothing |
void |
Given a Julia function:
@ccallable function mylib_add(a::Cdouble, b::Cdouble)::Cdouble
return a + b
endThe generator produces:
double mylib_add(double a, double b);-
Write Julia functions with
@ccallable:@ccallable function mylib_init()::Cint # Initialize library return 0 end
-
Generate C header:
julia parse_ccallable.jl mylib.jl
-
Use in C code:
#include "mylib.h" int main() { if (mylib_init() != 0) { return 1; } // Use other functions... return 0; }
-
Compile with juliac or Julia embedding:
gcc -o myapp main.c -L. -lmylib -ljulia
- The
@ccallablemacro is used to mark Julia functions that should be callable from C - All arguments and return types must be C-compatible types
- Arrays are passed as pointers with separate length parameters
- Strings use
Cstring(null-terminated) orPtr{Cchar}for buffers - Error handling typically uses integer return codes
- Julia 1.0 or later
- No external dependencies required
Hi! What license (if any) are the scripts under?