CBinding.jl icon indicating copy to clipboard operation
CBinding.jl copied to clipboard

importing #define numerical constants to Julia

Open hros opened this issue 3 years ago • 3 comments

is it possible to import #define NAME val from a C header file to Julia such that NAME = val?

hros avatar Nov 08 '22 01:11 hros

In general it is not possible without writing a very large amount of code. But some of the more trivial conversions (e.g. numerical constants) are attempted by CBinding.

julia> module Macros
         using CBinding

         c``

         c"""
           #define NUMBER 1234
         """j
       end
Main.Macros

julia> Macros.@NUMBER
1234

krrutkow avatar Nov 08 '22 13:11 krrutkow

fantastic. It is working for me. Edit: actually, it is working only for some macro constants. in my project (see link below), XXH3_SECRET_SIZE_MIN is recognized but XXH_SECRET_DEFAULT_SIZE is not.

One more thing, if you could have a look at the xxh project (referenced in a previous issue): I am trying to wrap one last function, and for some reason it is not being exposed by CBinding. The function is XXH3_generateSecret (and its variant: XXH3_generateSecret_fromSeed). The function prototype looks like the other functions which are detected and exported by CBinding.

Naturally, I can create a custom wrapper for the function, but it would be a shame to load the library with Libdl just for this function

hros avatar Nov 08 '22 15:11 hros

Related: using the cling interpreter to resolve #define values... https://github.com/JuliaInterop/Clang.jl/issues/309#issuecomment-1321300707

Cling is now available as a jll: https://github.com/JuliaBinaryWrappers/Cling_jll.jl Currently working on Linux and on intel macOS. Detail here: https://github.com/JuliaPackaging/Yggdrasil/blob/master/C/Cling/build_tarballs.jl#L102-L117

notinaboat avatar Nov 28 '22 12:11 notinaboat