# Seeking documentation on C calling Julia API

**URL:** https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348
**Category:** General Usage
**Tags:** c
**Created:** [November 9, 2018, 3:49pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348 "2018-11-09T15:49:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![peter.wolf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter.wolf/32/6018_2.png) [@peter.wolf](https://discourse.julialang.org/u/peter.wolf)
#### Post date: [November 9, 2018, 3:49pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348/1 "2018-11-09T15:49:02Z")

</div>

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

Here is my Julia

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

```

Following these directions

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

```julia
  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

---

<div class="post-metadata">

### Author: ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)
#### Post date: [November 9, 2018, 4:03pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348/2 "2018-11-09T16:03:37Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![peter.wolf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter.wolf/32/6018_2.png) [@peter.wolf](https://discourse.julialang.org/u/peter.wolf)
#### Post date: [November 9, 2018, 4:16pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348/3 "2018-11-09T16:16:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)
#### Post date: [November 9, 2018, 4:28pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348/4 "2018-11-09T16:28:29Z")

</div>

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](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.

---

<div class="post-metadata">

### Author: ![peter.wolf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter.wolf/32/6018_2.png) [@peter.wolf](https://discourse.julialang.org/u/peter.wolf)
#### Post date: [November 9, 2018, 4:36pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348/5 "2018-11-09T16:36:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)
#### Post date: [November 9, 2018, 5:00pm UTC](https://discourse.julialang.org/t/seeking-documentation-on-c-calling-julia-api/17348/6 "2018-11-09T17:00:44Z")

</div>

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](https://github.com/JuliaLang/PackageCompiler.jl).
