# CBinding error

**URL:** <https://discourse.julialang.org/t/cbinding-error/74688>\
**Category:** General Usage\
**Tags:** c\
**Created:** [January 15, 2022, 10:04pm UTC](https://discourse.julialang.org/t/cbinding-error/74688 "2022-01-15T22:04:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 15, 2022, 10:04pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/1 "2022-01-15T22:04:37Z")

</div>

@c\_str is exported by CBinding but doesn’t appear to want to show up.

Also in the examples for CBinding (all of the examples) there something like:

`using assimp_jll`

but that means i have to generate assimp\_jll, and there doesn’t seem to be any documentation on how to do that.

I’m using stdio.h to create a simple example, but nothing i try does anything but give me the @c\_str error.

```julia
julia> using CBinding

julia> c"""
       #include <stdio.h>
       """;
ERROR: LoadError: UndefVarError: @c_str not defined
in expression starting at REPL[2]:1

julia> 

```

---

<div class="post-metadata">

**Author:** ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)\
**Post date:** [January 16, 2022, 1:59pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/2 "2022-01-16T13:59:19Z")

</div>

The `*_jll` packages used in the examples are just existing Julia packages that provide libraries and headers, but CBinding doesn’t require those packages. You could creating bindings to system libraries (like what [System.jl](https://github.com/analytech-solutions/System.jl/tree/master/pkgs) demonstrates), or they could be to libraries you build from scratch (like [PLCTag.jl](https://github.com/libplctag/PLCTag.jl/blob/master/deps/build.jl) does).

What version of Julia are you running? CBinding v1 requires 1.5+, and due to the Julia package manager’s logic, running on an older Julia version will install the pre-v1 versions of CBinding which is a substantially different package.

On Julia 1.5+, you should get something like:

```julia
julia> using CBinding

julia> c"""
       struct S {
         int i;
       };
       """;
ERROR: LoadError: Compiler context (c`...`) not created before declaring C bindings (c"...") in Main
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] clang_str(mod::Module, loc::LineNumberNode, lang::Symbol, str::String, opts::String)
   @ CBinding ~/.julia/packages/CBinding/kBUap/src/context.jl:475
 [3] clang_str(mod::Module, loc::LineNumberNode, lang::Symbol, str::String)
   @ CBinding ~/.julia/packages/CBinding/kBUap/src/context.jl:452
 [4] var"@c_str"( __source__ ::LineNumberNode, __module__ ::Module, exprs::Vararg{Any, N} where N)
   @ CBinding ~/.julia/packages/CBinding/kBUap/src/context_c.jl:5
in expression starting at REPL[2]:1

```

And when correctly creating a (empty) C compiler context first:

```julia
julia> c``

julia> c"""
       struct S {
         int i;
       };
       """;

julia> c"struct S"(i = 1234)
var"(c\"struct S\")" (4-byte struct)
  i = 1234

```

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 16, 2022, 5:01pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/3 "2022-01-16T17:01:13Z")

</div>

Thanks for responding.  
I’m using 1.6.1

```julia
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.1 (2021-04-23)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using CBinding

julia> c"""
       struct S {
       int ;
       };
       """;
ERROR: LoadError: UndefVarError: @c_str not defined
in expression starting at REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 16, 2022, 5:08pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/4 "2022-01-16T17:08:11Z")

</div>

I installed 1.7.1 and I get the error message you indicate should be happening in 1.5  
strange…  
Here’s what the package manager told me when I added CBinding

```julia
    Updating `~/.julia/environments/v1.7/Project.toml`
  [d43a6710] + CBinding v1.0.8
    Updating `~/.julia/environments/v1.7/Manifest.toml`
  [d43a6710] + CBinding v1.0.8

```

```nohighlight
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.7.1 (2021-12-22)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using CBinding

julia> c"""
       struct S {
       int i;
       };
       """;
ERROR: LoadError: Compiler context (c`...`) not created before declaring C bindings (c"...") in Main
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] clang_str(mod::Module, loc::LineNumberNode, lang::Symbol, str::String, opts::String)
   @ CBinding ~/.julia/packages/CBinding/kBUap/src/context.jl:475
 [3] clang_str(mod::Module, loc::LineNumberNode, lang::Symbol, str::String)
   @ CBinding ~/.julia/packages/CBinding/kBUap/src/context.jl:452
 [4] var"@c_str"( __source__ ::LineNumberNode, __module__ ::Module, exprs::Vararg{Any})
   @ CBinding ~/.julia/packages/CBinding/kBUap/src/context_c.jl:5
in expression starting at REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)\
**Post date:** [January 16, 2022, 6:49pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/5 "2022-01-16T18:49:31Z")

</div>

Oh yes, if you are using a recent version of Julia, then perhaps another package you have installed could be requiring an older CBinding version, so you get stuck with that one. This type of problem was why I originally tried to make v1 a new package.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 16, 2022, 9:41pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/6 "2022-01-16T21:41:20Z")

</div>

Sorry, not following.

it looks like 1.7.1 pulled in CBinding 1.0.8

shouldn’t that be ok ?

---

<div class="post-metadata">

**Author:** ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)\
**Post date:** [January 17, 2022, 2:13am UTC](https://discourse.julialang.org/t/cbinding-error/74688/7 "2022-01-17T02:13:35Z")

</div>

That is good at the moment, but if you install a package like P4est.jl or Trixi.jl, then your CBinding will be downgraded to a pre-v1 version by the package manager in order to meet the requirements of P4est.jl.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 17, 2022, 4:27am UTC](https://discourse.julialang.org/t/cbinding-error/74688/8 "2022-01-17T04:27:05Z")

</div>

yes, but 1.7.1 + CBinding 1.0.8 is not working.

Any ideas as to why not ?

I’m happy to try things out, just not sure where to start.

---

<div class="post-metadata">

**Author:** ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)\
**Post date:** [January 17, 2022, 5:56pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/9 "2022-01-17T17:56:57Z")

</div>

What doesn’t work?

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 17, 2022, 5:58pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/10 "2022-01-17T17:58:52Z")

</div>

see my reply with your simple struct example in a 1.7.1 REPL.

it errors out, but gives the error you said should only occur in 1.5.x

---

<div class="post-metadata">

**Author:** ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)\
**Post date:** [January 17, 2022, 6:08pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/11 "2022-01-17T18:08:49Z")

</div>

Oh, no that is an error that is expected to occur because there was no compiler context created yet. The second block of code shows the correct usage as explained in the [README](https://github.com/analytech-solutions/CBinding.jl#create-bindings-with-cbindingjl).

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 17, 2022, 6:32pm UTC](https://discourse.julialang.org/t/cbinding-error/74688/12 "2022-01-17T18:32:31Z")

</div>

oh, good grief i missed the c``  
working now…  
thank you so much for your patience and help !
