PackageCompiler @ccallable for julia function that take String's as arguments

I have a C/C++ based simulation. I want to package Julia Code that loads a custom data-base type object and does some fast lookups and quick calculations so it is accessible to C. So I’m trying to set things up to make PackageCompiler happy. I want Julia to manage the loading of the database which will hang around as a Ref. The C will call various julia functions to set the conditions that pertain to the next lookup and then make a separate call to lookup and calculate the results. Then C will call again for the answer.

The problem I have is, the julia functions that load and initialize the database, need string arguments to find the files. I tried to setup the @ccallable macro by specifying the input would come in as a CString.

Base.@ccallable function julia_load_dB_jld2(jld2_filename::Cstring)::Cint
  blah blah blah

But really that function needs jld2_filename to be a string.

I setup some unit tests to test this and did something like this to test it.


But Cstring I guess is just a pointer and julia’s String doesn’t know how to convert it.

So how do I get julia_load_dB_jld2 to be able to use this as a String?

I feel this is so basic it must be covered, but I find it nowhere. Do I just have the paradigm incorrect in my head.

Best Regards,
Allan Baker

A String in Julia is not the same as a string in C; the latter is (most commonly) just a char* (which is what Cstring is), while the former is a more complicated type, since the length of the string is stored in front of the data. You’ll have to either create a string with unsafe_string in Julia (assuming you have the data as a char* in C), or create the String object through the C-API.

Are you looking for Base.unsafe_string(jld2_filename) to turn the CString back into a useful object?

Yes. I think that is it, at least I think it will allow the unit testing to complete before I try making the C-libs with PackageCompiler. I’m having trouble finding the complete thread of this workflow in any of the documentation. It’s probably there, but my google skills must be lacking. Sometimes, the documentation expects a higher level of experience, but this is my first time interfacing to C or using the PackageCompiler.