CBinding error

@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> using CBinding

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

julia> 

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 demonstrates), or they could be to libraries you build from scratch (like PLCTag.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> 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> c``

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

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

Thanks for responding.
I’m using 1.6.1

   _       _ _(_)_     |  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

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

    Updating `~/.julia/environments/v1.7/Project.toml`
  [d43a6710] + CBinding v1.0.8
    Updating `~/.julia/environments/v1.7/Manifest.toml`
  [d43a6710] + CBinding v1.0.8
   _       _ _(_)_     |  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

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.

Sorry, not following.

it looks like 1.7.1 pulled in CBinding 1.0.8

shouldn’t that be ok ?

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.

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.

What doesn’t work?

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

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.

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