CBinding.jl - bringing C's nested structs/unions, alignment, bitfields, variadic functions to Julia

I am announcing the availability of package CBinding.jl as a much improved way of interfacing C APIs from Julia with syntax analogous to the C you are binding to!

julia> using CBinding
julia> @cstruct MyCStruct {    # struct MyCStruct {
           c::Cchar            #     char c;
           i::Cint             #     int i;
           @cunion {           #     union {
               f::Cfloat       #         float f;
               d::Cdouble      #         double d;
           }                   #     };
       }                       # };
julia> @cstruct BitfieldStruct {    # struct BitfieldStruct {
           i::Cint:2                #     int i:2;
           j::Cuint:2               #     unsigned int j:2;
       }                            # };

julia> lib = Clibrary()  # dlopens the Julia process
Clibrary(Ptr{Nothing} @0x000061eefd6a1000)

julia> func = Cfunction{Cint, Tuple{Cstring, Vararg}}(lib, :printf)    # int printf(char *, ...)
Ptr{Cfunction{Nothing,Tuple{Cstring,Vararg{Any,N} where N}}} @0x000061eefc388930

julia> func("%s i%c %ld great demo of CBinding.jl v%3.1lf%c\n", "this", 's', 1, 0.1, '!')
this is 1 great demo of CBinding.jl v0.1!

Iā€™m looking to get community feedback on how well it works on various platforms (only tested on Linux x86_64 to this point) and what syntax or package improvements might be desired. If you interface with C libraries from Julia, then please check out this package and help make it a great addition to the ecosystem! Thank you!

14 Likes