calling julia functions in C++

709 views
Skip to first unread message

Kostas Tavlaridis-Gyparakis

unread,
Jun 30, 2015, 10:16:04 AM6/30/15
to julia...@googlegroups.com
Hello,
I am trying to write some function in Julia which I will be able to call inside my C++ projects in Eclipse.
In the documentation there is this example on how to call a function of julia from an existing module.
So, what I have done was to create my own module where inside I included my function and then my
understanding is that I should be using "jl_new_module(jl_sym_t *name);" instead of jl_base_module.
But I am not sure (in case my assumption that this is the correct command is true) how to proper use
it's syntax, as what I am trying is:

        jl_function_t * func = jl_get_function(jl_new_module(mymodule),"myfunction");

and I tried instead of mymodule also mymodule.jl and "mymodule" and "mymodule.jl" and in all the attempts I
recieve an error that:

mymodule  was not declared in this scope


Let me note beforehand that the module is being stored globaly as when I run julia on the terminal I can use it
through the comand using mymodule.
Still maybe I am trying to use the wrong command or sth, so if there is any suggestion I would be really greatful
to hear it.



Isaiah Norton

unread,
Jun 30, 2015, 10:28:54 AM6/30/15
to julia...@googlegroups.com
`jl_new_module` creates a new module. You must evaluate your .jl file defining the module first, then to get a reference to the module do:

`jl_value_t* mod = jl_eval_string("MyModName");

Then you can pass "mod" as the argument to `jl_get_function`.

Kostas Tavlaridis-Gyparakis

unread,
Jun 30, 2015, 10:54:41 AM6/30/15
to julia...@googlegroups.com
Sorry but I am not sure what you mean and how to "evaluate your .jl file defining the module first"?
(I am really new to Julia so maybe the question sounds really stupid, sorry for that)

Isaiah Norton

unread,
Jun 30, 2015, 11:32:53 AM6/30/15
to julia...@googlegroups.com
Sorry but I am not sure what you mean and how to "evaluate your .jl file defining the module first"?

Ok, say you have a file:

```
module mymod
function foo() ... end
end
```

At the Julia prompt you would do:

julia> include("myfile.jl")

And then you have the module `mymod` available in the global namespace. In C you can do the equivalent with:

`jl_load("myfile.jl")`

I am really new to Julia so maybe the question sounds really stupid, sorry for that

No problem, no stupid questions. However, I would suggest that you might want to spend some time getting really familiar with Julia by itself, before trying to use the embedding API. It might save a lot of time in the long run.

Kostas Tavlaridis-Gyparakis

unread,
Jun 30, 2015, 11:47:45 AM6/30/15
to julia...@googlegroups.com
Ok, so first of all thanks a lot for all the help so far.
So, now I try to follow the instructions and I write the following three lines of code inside C++:

 jl_load("mymodule.jl");
        jl_value_t * mod = jl_eval_string("mymodule");
        jl_function_t * func = jl_get_function(jl_new_module(mod),"myfunction");

(the jl file and the module itself have the same name in this case mymodule)
But I do receive the following 2 errors when Eclipse compiles:

1) error: invalid conversion from ‘void*’ to ‘jl_value_t* {aka _jl_value_t*}’ [-fpermissive] (this is referring to  jl_value_t * mod = jl_eval_string("mymodule");)

2) error: cannot convert ‘jl_value_t* {aka _jl_value_t*}’ to ‘jl_sym_t* {aka _jl_sym_t*}’ for argument ‘1’ to ‘jl_module_t* jl_new_module(jl_sym_t*)’ (this referring to jl_function_t * func = jl_get_function(jl_new_module(mod),"myfunction");)



>No problem, no stupid questions. However, I would suggest that you might want to spend some time getting really familiar with Julia by itself, before trying to use the embedding API. It might save a lot of time in the long run.

You are totally right on this, I am just trying first to check if it is doable to do some combinations between C++ in Eclipse and Julia (such as using functions written in
Julia inside a C++ routine etc), because I am planning to connect a large-scale C++ with Julia and before starting to studying Julia in full detail and start writing proper
code was thinking to do some small tests in connectivity between the two. But it turns out that I don't know some very basic things to finish this task.

Isaiah Norton

unread,
Jun 30, 2015, 11:58:18 AM6/30/15
to julia...@googlegroups.com
try

jl_value_t * mod = (jl_value_t*)jl_eval_string("mymodule");

jl_function_t * func = jl_get_function((jl_module_t*)mod,"myfunction");

(jl_new_module creates a new module -- that's not what you want, because the module containing your function is created when you eval "yourfile.jl")

Kostas Tavlaridis-Gyparakis

unread,
Jun 30, 2015, 12:26:18 PM6/30/15
to julia...@googlegroups.com
Ok, thanks a lot it did work just fine!
just one more quick question, if I got it right jl_call1 is for adding one input argument to the functions and jl_call2 is for adding two input arguments to the function.
For adding more arguments I continue in the same way? For instance for 4 arguments I just use use jl_call4?
Or what I am saying is completely mistaken?

Isaiah Norton

unread,
Jun 30, 2015, 12:32:44 PM6/30/15
to julia...@googlegroups.com
cool. you can go up to jl_call3. after that you need to pack the args in a jl_value_t** and use the general form  `jl_call(f, args, nargs)` ... there should be a lot of examples how to do that if you grep through src/ for "jl_call".

Tom Breloff

unread,
Jun 30, 2015, 1:52:46 PM6/30/15
to julia...@googlegroups.com
I remember a post recently about improved performance when packing many function arguments into an immutable for function calling.  Is this (jl_call1 vs jl_call) the reason?

Jeff Waller

unread,
Jun 30, 2015, 10:16:13 PM6/30/15
to julia...@googlegroups.com


On Tuesday, June 30, 2015 at 1:52:46 PM UTC-4, Tom Breloff wrote:
I remember a post recently about improved performance when packing many function arguments into an immutable for function calling.  Is this (jl_call1 vs jl_call) the reason?

No that's just for convenience.  

Also, if possible, try to void jl_eval_string as it's comparably slow to its counterparts.

 

K leo

unread,
Sep 8, 2016, 12:09:38 AM9/8/16
to julia-users
Thank you.  This just saved my day.  Can someone please put this intro in the documentation?

Isaiah Norton

unread,
Sep 8, 2016, 1:26:54 PM9/8/16
to julia...@googlegroups.com
As far as I can tell, everything in this thread is covered in the embedding section [http://docs.julialang.org/en/latest/manual/embedding/] except for the note about `jl_load` usage. Care to make a pull-request? ;)

K leo

unread,
Sep 8, 2016, 6:30:59 PM9/8/16
to julia-users
jl_load and the following two are not in the documentation.  Specifically how to call function in one's own module.

jl_value_t * mod = (jl_value_t*)jl_eval_string("mymodule");
jl_function_t * func = jl_get_function((jl_module_t*)mod,"myfunction");


Reply all
Reply to author
Forward
0 new messages