# How to call a custom julia function from c++ (a custom locally created .jl file)

**URL:** <https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708>\
**Category:** New to Julia\
**Tags:** question, c\
**Created:** [September 5, 2021, 7:23am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708 "2021-09-05T07:23:36Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 5, 2021, 7:23am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/1 "2021-09-05T07:23:37Z")

</div>

So I wrote a julia file myself, called function.jl, and I want to load it in C++ and call functions in it. That’s it.  
I can’t find anything in the documentation about this. The docs cover jl\_eval\_string and jl\_get\_function for a library function eg,

```julia
jl_get_function(jl_base_module, "sqrt")

```

but my file is not part of jl\_base\_module. It’s something I made up myself.  
I don’t want to ask too much of a leading question because there might be more than one way to call a function in a custom .jl file.  
Googling the subject I found jl\_load which is pretty much non-documented and not sure if it’ll help.  
Googling also yields a lot of conflicting suggestions, probably due to changes to julia over time.  
I found this which produces an access violation in jl\_get\_function:

```julia
jl_eval_string("include(\"function.jl\")");
jl_value_t *mod = jl_eval_string("Fun");
jl_function_t *add = jl_get_function((jl_module_t*)mod,"add");

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/c/fce8533fd20b2c5a4c3ee8412305c03175115d7e.png)

---

<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:** [September 5, 2021, 7:37am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/2 "2021-09-05T07:37:28Z")

</div>

What does `function.jl` contain?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 5, 2021, 7:45am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/3 "2021-09-05T07:45:33Z")

</div>

If you set it up so that `function.jl` loads your function in the the Main module you can make use of `jl_main_module`.

You can get some more inspiration from [https://github.com/JuliaLang/julia/blob/master/test/embedding/embedding.c](https://github.com/JuliaLang/julia/blob/master/test/embedding/embedding.c).

For my part I approach embedding somewhat differently, as documented in [GitHub - GunnarFarneback/DynamicallyLoadedEmbedding.jl: Embed Julia with dynamical loading of libjulia at runtime.](https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl).

---

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 5, 2021, 8:19am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/4 "2021-09-05T08:19:29Z")

</div>

A problem with these examples is that I specifically want to load scripts from files in a local directory (like function.jl, but any arbitrary script). Writing scripts in strings in C is a non-starter.

EDIT: I could do fopen and fread to load the file I want to use into memory, then call eval string as in the examples. I thought there would be a more elegant way.

---

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 5, 2021, 8:23am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/6 "2021-09-05T08:23:41Z")

</div>

> [@Sukera](#):
>
> What does `function.jl` contain?

Is it relevant? The point of what I’m trying to do is load arbitrary script files from directories in my local drive. I’m new to this and looking for guidance. How about:

```julia
module Fun
    add(a,b) = a+b
end

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 5, 2021, 9:04am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/7 "2021-09-05T09:04:10Z")

</div>

It’s of course possible to correctly retrieve the `Fun` module but I don’t know how to do that out of hand, so I’ll leave that to someone else.

As an alternative you could just skip the `module Fun` layer in your `function.jl` so that `add` gets defined directly in `Main` or do

```julia
jl_eval_string("include(\"function.jl\"); using .Foo: add");

```

to load it into `Main`. Then you can retrieve your function with `jl_get_function(jl_main_module, "add")`.

---

<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:** [September 5, 2021, 9:12am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/8 "2021-09-05T09:12:21Z")

</div>

Well, it matters insofar that if the file doesn’t contain a module, all functions are placed in the default `Main` module.

Despite julia being dynamic and looking like a scripting language, it’s compiled, so keep that in mind when trying to integrate it into a game.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 5, 2021, 5:59pm UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/9 "2021-09-05T17:59:26Z")

</div>

I would generally recommend (a) putting your Julia code into a module/package and (b) using `cfunction` to create a C++ function pointer from Julia. See the links in [https://github.com/JuliaLang/julia/issues/38932](https://github.com/JuliaLang/julia/issues/38932)

---

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 5, 2021, 8:03pm UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/10 "2021-09-05T20:03:58Z")

</div>

> [@stevengj](#):
>
> putting your Julia code into a module/package

This bit seems like it’s tribal knowledge at the moment. From C++, how do I read and call functions that I have in my own modules and/or packages? The examples I’ve found only call existing Julia functions and special functions.

The examples using cfunction are great. Thanks for that!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 5, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/11 "2021-09-05T20:23:49Z")

</div>

> [@solaris](#):
>
> From C++, how do I read and call functions that I have in my own modules and/or packages? The examples I’ve found only call existing Julia functions and special functions.

The same as for the [SpecialFunctions example](https://scicomp.stackexchange.com/questions/23194/i-am-searching-for-a-c-code-implementing-the-complex-polygamma-function/23733#23733) you found: just call `jl_eval_string("import MyModule");`

---

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 6, 2021, 12:33am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/12 "2021-09-06T00:33:22Z")

</div>

I get this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/9/a99ab302c4882bfba4403abb83c93a1e22af635c.png)

ret = nullptr  
The difficulty here is that all I get is a null. No errors or warning or any kind of output that helps narrow down the problem.

typing the @cfunction line in julia REPL with the module already imported and working yields:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/6/567bfb5db630ad71795f64c158019da352e26038.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 6, 2021, 12:50am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/13 "2021-09-06T00:50:43Z")

</div>

Do the Julia commands you are evaluating with eval\_string work in the REPL?

(To get more informative error messages in C++, you need to add some exception-handling code, but it is a lot easier to debug in the REPL.)

PS. Don’t post screenshots of code. Post quoted text.

---

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 6, 2021, 12:51am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/14 "2021-09-06T00:51:24Z")

</div>

Yes, in REPL all commands work except the @cfunction:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/6/567bfb5db630ad71795f64c158019da352e26038.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 6, 2021, 12:53am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/15 "2021-09-06T00:53:16Z")

</div>

That’s because you are calling `@cfunction` incorrectly. Get it to work in the REPL first.

---

<div class="post-metadata">

**Author:** ![solaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/solaris/32/28875_2.png) [@solaris](https://discourse.julialang.org/u/solaris)\
**Post date:** [September 6, 2021, 3:02am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/16 "2021-09-06T03:02:19Z")

</div>

I got it working, thanks!  
one weird thing is that my func only had one argument:

```julia
function foo(x::Int)
    return x * x
end

```

@cfunction didn’t allow just one argument no matter what. I kept getting, “argument types must be a literal tuple”. So I had to do:  
@cfunction(foo, Int, (Int,Int))  
event though there’s only one argument.  
I also made a function that didn’t return anything, which @cfunction also didn’t like so I had to return ‘Int’ no matter what.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 6, 2021, 3:04am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/17 "2021-09-06T03:04:41Z")

</div>

> [@solaris](#):
>
> @cfunction didn’t allow just one argument no matter what. I kept getting, “argument types must be a literal tuple”.

To make a tuple with a single element you need a trailing comma.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 6, 2021, 3:43am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/18 "2021-09-06T03:43:14Z")

</div>

> [@stevengj](#):
>
> need a trailing comma.

specifically consider the difference here:

```julia
julia> typeof((:foo))
Symbol

julia> typeof((:foo,))
Tuple{Symbol}

```

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [September 6, 2021, 6:06am UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/19 "2021-09-06T06:06:44Z")

</div>

> [@solaris](#):
>
> I also made a function that didn’t return anything, which @cfunction also didn’t like so I had to return ‘Int’ no matter what.

Julia always returns the last expression.

```julia
julia> function foo()
       4
       end
foo (generic function with 1 method)

julia> a = foo()
4

julia> a
4

julia>

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 6, 2021, 12:35pm UTC](https://discourse.julialang.org/t/how-to-call-a-custom-julia-function-from-c-a-custom-locally-created-jl-file/67708/20 "2021-09-06T12:35:20Z")

</div>

> [@Tero\_Frondelius](#):
>
> > [@solaris](#):
> >
> > I also made a function that didn’t return anything, which @cfunction also didn’t like so I had to return ‘Int’ no matter what.
> 
> Julia always returns the last expression.

The equivalent of a C/C++ function with no (`void`) return value is to `return nothing` in the Julia function.
