# Creating functions exposed to the global scope dynamically?

**URL:** <https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874>\
**Category:** General Usage\
**Created:** [October 16, 2021, 2:49am UTC](https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874 "2021-10-16T02:49:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Green](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_green/32/32265_2.png) [@Chris\_Green](https://discourse.julialang.org/u/Chris_Green)\
**Post date:** [October 16, 2021, 2:49am UTC](https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874/1 "2021-10-16T02:49:20Z")

</div>

I wanted to use julia+IJulia to easily add a notebook interface/REPL to my large C++ program.

I wanted it to be really easy and non-intrusive on the c++ side, and in particular to add _no_ dependencies to the c++ build process. IE, the c++ code would just be a DLL that Julia could load and call into.

Question #1:  
If you load a dll from Julia, is there any way to call a function in it, passing some sort of context pointer that would let that c++ code interact with the interpreter. In particular to get it to be able to export symbols in a a way similar to what you can do with julia,h if you are embedding Julia in your app (as opposed to embedding your app in julia which is what i am doing).

Something like (on the c side)  
extern “C” \_\_declspec( dllexport ) AddExports( something \*p )  
{  
// Julia loads our dll and calls this with ccall, passing some ref to the interpreter  
Do something with p that lets us add a c function to the interpreter();  
}

Question #2, I don’t care as much about question #1 as #2.  
What i actually want to do is this. I want Julia code to look at a list of exported function prototypes via the dll (stored as strings+entry points), and create functions for them. This lets me have a generic header-only export interface that isn’t julia-dependent, it’s just a convention. So I coded the base of it quickly in one stand alone file:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/e/4e3424a4ea95ba715a1ad7ce37d3884b9141a3ac.png)

That’s the C++ code that makes a test dll for Julia to load.  
I then started to write something quick to load it and see if I could write code to create the exported functions:

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

So, if I compile my test lib to a .dll.  
I load Julia, and include the code above.  
I do

mylib=ImportCPP( “test.dll” )  
CreateExportedFunctions( mylib)

Nnow, I would like to be able to do this:  
HelloWorld()

and it works, in that it prints out the names of the 3 functions exported in the c code.  
The names are exported as c++ prototype strings that should be easy to parse in julia to create bindings for.

The question is _what_ to do after the line that prints “found voidfn”? This line successfully finds the prototype HelloWorld() and knows how to call it with “ccall”.  
What I would like for this line to do is create _something_ called HelloWorld, which could be called after. Would this be a closure? Or would I use metaprogramming to create the function body? Either way in some sense, some symbol/variable needs to be created called HelloWorld, such that if I then type HelloWorld() into the repl, it will end up doing a ccall into the entry point in my dll.

I guess ultimately, I would like to be able to do something like:

mylib=ImportCPP( "test.dll )  
CreateExportedFunctions( mylib, :MyLIb ) // make a new module  
MyLib.HelloWorld()  
using MyLib  
HelloWorld()

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [October 16, 2021, 3:19am UTC](https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874/2 "2021-10-16T03:19:01Z")

</div>

I guess you need to make a macro like what ModernGL.jl did here:

[https://github.com/JuliaGL/ModernGL.jl/blob/f81e7ea6a1df8bff7fccd4708cd070fdf93e5ec5/src/functionloading.jl#L28-L71](https://github.com/JuliaGL/ModernGL.jl/blob/f81e7ea6a1df8bff7fccd4708cd070fdf93e5ec5/src/functionloading.jl#L28-L71)

If your large C/C++ program does not use non-builtin datatypes(like OpenGL), then it’s ok to follow this way. Otherwise, you need to map datatypes, you could also make everything an opaque pointer though.

---

<div class="post-metadata">

**Author:** ![Chris\_Green](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_green/32/32265_2.png) [@Chris\_Green](https://discourse.julialang.org/u/Chris_Green)\
**Post date:** [October 16, 2021, 3:30am UTC](https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874/3 "2021-10-16T03:30:43Z")

</div>

I’m not very worried about mapping data types as I can make my export function generate wrappers to map things into and out of a few common types, and I figured I could also export them from the dll if I want to get fancy.

Thanks for the reference to the opengl bindings, I’ll take a look. The gist is that they build a symbolic expression declaring a function, and then evaluate it to get the function created?

I want to handle reload, so I’d make my generated bindings check for stale function pointers in some way.

---

<div class="post-metadata">

**Author:** ![Chris\_Green](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_green/32/32265_2.png) [@Chris\_Green](https://discourse.julialang.org/u/Chris_Green)\
**Post date:** [October 16, 2021, 6:16am UTC](https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874/4 "2021-10-16T06:16:09Z")

</div>

I guess I know the answer to #1. You can just define a julia function that does the operations to create a new symbol in the Julia execution environment, create a c-callable function pointer to it, and then call some entry point in the dll, passing that pointer.

---

<div class="post-metadata">

**Author:** ![Chris\_Green](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris_green/32/32265_2.png) [@Chris\_Green](https://discourse.julialang.org/u/Chris_Green)\
**Post date:** [October 16, 2021, 9:23pm UTC](https://discourse.julialang.org/t/creating-functions-exposed-to-the-global-scope-dynamically/69874/5 "2021-10-16T21:23:26Z")

</div>

Thanks, I got this working, including dynamic reload!

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