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

**URL:** https://discourse.julialang.org/t/packagecompiler-ccallable-for-julia-function-that-take-strings-as-arguments/104507
**Category:** General Usage
**Tags:** package-compiler
**Created:** [October 2, 2023, 5:59pm UTC](https://discourse.julialang.org/t/packagecompiler-ccallable-for-julia-function-that-take-strings-as-arguments/104507 "2023-10-02T17:59:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)
#### Post date: [October 2, 2023, 5:59pm UTC](https://discourse.julialang.org/t/packagecompiler-ccallable-for-julia-function-that-take-strings-as-arguments/104507/1 "2023-10-02T17:59:29Z")

</div>

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.

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

```

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.

```julia
julia_load_dB_jld2(Base.unsafe_convert(Cstring,"../data/example_database.jld2"))  

```

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

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 2, 2023, 6:31pm UTC](https://discourse.julialang.org/t/packagecompiler-ccallable-for-julia-function-that-take-strings-as-arguments/104507/2 "2023-10-02T18:31:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)
#### Post date: [October 2, 2023, 6:41pm UTC](https://discourse.julialang.org/t/packagecompiler-ccallable-for-julia-function-that-take-strings-as-arguments/104507/3 "2023-10-02T18:41:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)
#### Post date: [October 2, 2023, 7:09pm UTC](https://discourse.julialang.org/t/packagecompiler-ccallable-for-julia-function-that-take-strings-as-arguments/104507/4 "2023-10-02T19:09:39Z")

</div>

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.
