Seeking documentation on C calling Julia API


#1

Hello, I am still trying to call Julia from C++

Here is my Julia

module TestF
f(x::Int32)::Int32 = x + 3
end

Following these directions

[https://docs.julialang.org/en/v1/manual/embedding/index.html](http://Manual >> Embedding Julia)
[https://groups.google.com/forum/#!topic/julia-users/27KVtFujpFY](http://calling julia functions in C++)
I am calling

  jl_init();
  double ret_unboxed = 0;
  jl_value_t *mod = jl_eval_string("TestF");
  if(mod) {
    jl_function_t *func = jl_get_function((jl_module_t*)mod, "f");
    if(func) {
      jl_value_t *argument = jl_box_int32(x);
      jl_value_t *ret = jl_call1(func, argument);
      ret_unboxed = jl_unbox_int32(ret);

Unfortunately, I get 0 for both mod and func.

First question: is jl_eval_string() really the best way to get the module?
Second question: can anyone point me to more documentation and/or a real working example of calling Julia functions from C?

Many thanks
Peter


#2

If you get 0 for both mod and func, then there seems to be something wrong with the execution of the julia code module Test F .... end in the embedding julia. And there is no code in your example which does this.
Did you forget to execute module Test F ... end after jl_init()? Or if you did it, what was your method?


#3

That is all my C code. How do I execute my module?


#4

You could use something like jl_eval_string("Base.include(Main, the location of your julia file)") to execute your module for julia 0.7 and 1.0, or jl_eval_string("include(the location of your julia file)") for julia 0.6. And then you should be able to find your module and your function.
Plus, this is an R package of mine which embeds julia in R through c/cpp, https://github.com/Non-Contradiction/JuliaCall, but the embedding related code is separated in R/cpp/julia codes, so it may not be a very good first example.


#5

Thanks, but I am now confused.

What is the point of compiling and linking my Julia code if I am just going to invoke the REPL?

PackageCompiler produces a 14MB library from my tiny code. Surely this must include everything I need already compiled and ready to go.


#6

If you have already use PackageCompiler for your module, then it should be fine just by linking the generated shared library to your c code or using the generated new system image as far as I know. I think the best examples are in the repo itself: https://github.com/JuliaLang/PackageCompiler.jl.