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,
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:
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.
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:
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
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!
I got it working, thanks!
one weird thing is that my func only had one argument:
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.