-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
selectively exposing C functions from header file #114
Comments
A C function is bound to Julia as a singleton type ( Unfortunately, there are often header files containing prototypes to functions which do not actually exist in the library (or defined in a sneaky manner, like GLIBC sometimes does). So in essence, the header file is lying and claiming the library will contain something it doesn't, but from our perspective we do not know if it is the header file that is wrong or the library (or perhaps a library we are missing). So the warning is the indication that such a scenario exists. It is not possible to selectively import symbols, but if the warnings are fine to ignore, then you can add the |
@krrutkow, thank you so much for your great work on the package and the "hand holding" of developers I will ignore the warnings. # 0 methods for type constructor and
|
The README gives some examples of calling the functions. Since your |
The functions have pointers to data as arguments |
Depends on how the function is using the pointer, but something like this should be possible. using CBinding
c``
c"""
struct S {
int i;
int j;
};
inline static void func_name(struct S *a, struct S *b) { *a = *b; };
"""w
ptr = Libc.malloc(c"struct S") # malloc an instance (uninitialized)
ref = Ref(c"struct S"()) # create a zero-ed instanced
@info ptr[] # [ Info: var"(c\"struct S\")"(i=65713851, j=0)
c"func_name"(ptr, ref)
@info ptr[] # [ Info: var"(c\"struct S\")"(i=0, j=0) |
Thanks julia> c"XXH32_createState"()
ERROR: could not load symbol "XXH32_createState":
julia: undefined symbol: XXH32_createState |
Hmm, it works for me... julia> module libxxhash
using CBinding
using xxHash_jll
c`-I $(xxHash_jll.artifact_dir)/include -L $(xxHash_jll.artifact_dir)/lib -lxxhash`
const c"uint64_t" = UInt64
const c"uint32_t" = UInt32
const c"size_t" = Csize_t
c"""
#include <xxhash.h>
"""j
end
Main.libxxhash
julia> state = libxxhash.XXH32_createState()
CBinding.Cptr{Main.libxxhash.var"c\"struct XXH32_state_s\""}(0x0000000005902f00) |
First, thanks. I see that you are not using the c"XXH32_createState". I thought that this is the generated function that should be called (following your previous example: When I create the Julia wrapper functions, I am using If it is the latter (not using the functions in the jll package), then I will stick to manual wrapping functions. |
Because I used the julia> using .libxxhash
julia> libxxhash.XXH32_createState === c"XXH32_createState"
true
This is exactly what is happening, but with a level of indirection in place. The function signature (captured in the julia> supertype(typeof(c"XXH32_createState"))
Cbinding{Cfunction{Cptr{var"c\"struct XXH32_state_s\""}, Tuple{}, :cdecl}, Symbol("/home/user/.julia/artifacts/13ab51aa94fca17cd1fbd3b9ce14f303e481893e/lib/libxxhash"), :XXH32_createState} So when calling
That tells CBinding what libraries to look in (and also where to look for headers/libraries) for the bindings to find their symbols in and bind to, as shown above.
I'm not sure what you mean by "automatic parsing", but CBinding does automatically generate all of the types and function bindings for the header and library in |
Brilliant |
To quote Steve Jobs: "one more thing" typedef enum {
XXH_OK = 0, /*!< OK */
XXH_ERROR /*!< Error */
} XXH_errorcode; When calling the functions in julia, I see the following return value: var"(c\"XXH_errorcode\")" (0x00000000)
->c"XXH_OK" (0x00000000)
c"XXH_ERROR" (0x00000001) I can compare the result to either var"(c\"XXH_errorcode\")" (2 options)
c"XXH_OK" (0x00000000)
c"XXH_ERROR" (0x00000001) I understand that the Also looking at methods of functions, for example the following C function: XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length) I see the following method prototype: # 2 methods for callable object:
[1] (func::Cbinding_XXH32_update)(var"c\"statePtr\"", var"c\"input\"", var"c\"length\"") in Main at ...
[2] (cb::Cbinding{<:Cfunction})(args...) in CBinding at ... why does |
Because Julia lacks support for circular type references (JuliaLang/julia#269), and that is encountered a lot in C type definitions, the generic approach taken by CBinding is to do some sneaky maneuvering using abstract types, which are actually named to match the C types ( All the trickery will go away someday when Julia has
Well, technically the argument types are all If you just want to reference the C argument/return types for a binding, you can use help?> libxxhash.XXH32_createState
XXH32_state_t *XXH32_createState()
Defined at xxhash.h:481 (file:///home/user/.julia/artifacts/13ab51aa94fca17cd1fbd3b9ce14f303e481893e/include/xxhash.h)
Allocates an XXH32_state_t.
Details
=========
Must be freed with XXH32_freeState().
Returns
=========
An allocated XXH32_state_t on success, `NULL` on failure.
|
I am upgrading a package to use
CBinding
's automatic parsing of C header files (previously I performed this manually usingctypedef
to define structures and writing inline julia functions to performccall
to the C functions).The structs are imported perfectly (almost magically ;) ), and I am seeing for each of the C functions two new elements in the module:
c"function_name"
- seemds to be undefined when checking its type (although it is listed as part of the module)Cbinding_function_name
of typeUnionAll
Cbinding
as Julia functions?If so, please link to a usage example?
c"function_name"
,Cbinding_function_name
Cbinding
?Failed to find <C symbol> in: or the Julia process
. What do they meanthanks
The text was updated successfully, but these errors were encountered: