# Is it possible for "ccall" to call the shared library created by PackageCompiler.jl?

**URL:** <https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298>\
**Category:** General Usage\
**Tags:** ccall, package-compiler\
**Created:** [July 16, 2022, 3:14am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298 "2022-07-16T03:14:08Z")\
**Posts on this page:** 14\
**Page:** 3

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [July 21, 2022, 8:48pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/41 "2022-07-21T20:48:31Z")

</div>

Thank you!  
So i have to make the `.lib`, and following [this guide](https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170) it should be possible to statically use the dll in an app.

Another option, that is more similar to what I have done in Matlab, would be to to use [dynamic linking at run-time](https://docs.microsoft.com/en-us/windows/win32/dlls/using-run-time-dynamic-linking)

---

<div class="post-metadata">

**Author:** ![lionisxn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionisxn/32/37989_2.png) [@lionisxn](https://discourse.julialang.org/u/lionisxn)\
**Post date:** [July 22, 2022, 2:52am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/42 "2022-07-22T02:52:46Z")

</div>

I followed your example code to edit my project, and it worked, but subject to the same error. I’ll appreciate it if you could share your new progress about our similar issue.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 22, 2022, 5:30pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/43 "2022-07-22T17:30:38Z")

</div>

Could you be more specific? Are you encountering the error that occurs when you try to shutdown and reinitialize Julia? If so, the solution is do not shutdown Julia unless you are planning to exit the process (e.g. MATLAB).

---

<div class="post-metadata">

**Author:** ![lionisxn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionisxn/32/37989_2.png) [@lionisxn](https://discourse.julialang.org/u/lionisxn)\
**Post date:** [July 24, 2022, 9:13am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/44 "2022-07-24T09:13:27Z")

</div>

I write code like  
"  
addpath(genpath(binPath), ‘-end’);  
savepath;  
libname = fullfile(binPath, dllName);  
hfile = fullfile(inludePath, headh);  
loadlibrary(libname, hfile);  
if libisloaded(dllName)  
disp(["the library “, dllName, " is loaded successfully!”])  
end  
libfunctions(dllName);  
calllib(dllName, ‘init\_julia’, 0, {‘’});  
calllib(dllName, funName, 7232, 1); % to realize 7232 + 1  
calllib(dllName, ‘shutdown\_julia’, 0);  
unloadlibrary(dllName);  
"  
The code can be executed in step-by-step mode. But when the script is executed in the command window, matlab will shut down unexpectedly.

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [July 24, 2022, 11:19am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/45 "2022-07-24T11:19:06Z")

</div>

If you are referring to the issue were it can not be run twice in the same Matlab session, I had this issue too.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 24, 2022, 2:46pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/46 "2022-07-24T14:46:29Z")

</div>

> [@lionisxn](#):
>
> calllib(dllName, ‘shutdown\_julia’, 0);  
> unloadlibrary(dllName);

Do not call the `shutdown_julia` function until you plan on exiting MATLAB.

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [July 24, 2022, 6:43pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/47 "2022-07-24T18:43:46Z")

</div>

I have to say, I feel this should be possible and is an issue of Julia and not Matlab.

If I have time, I will try if the same behaviour can be observed with other callers e.g. a C++ or C# app or even another Julia session.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 25, 2022, 12:55am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/48 "2022-07-25T00:55:04Z")

</div>

> [@ohmsweetohm1](#):
>
> is an issue of Julia and not Matlab.

It is a Julia issue. The embedding mechanism is not designed for multiple embeddings. Running the atexit hooks is meant to do things such as flushing buffers or unlocking files. It is not necessarily tearing down and explicitly freeing every structure.

I’m not sure why it should be possible to repeatedly shutdown and initialize Julia within the same process. Java is another example where this restarting the runtime is not supported.

I suppose if you really wanted to do this, you could start a MATLAB worker and call this just before you terminate the worker. Another option would be to do the embedding via a mex file and call this during mexAtExit:

> **[Register function to call when MEX function clears or MATLAB terminates - MATLAB](https://www.mathworks.com/help/matlab/apiref/mexatexit.html)**
>
> Use mexAtExit to register a function to call just before clearing the MEX function or terminating MATLAB.

There might still have a similar issue if you try to reload the mex file.

I will refer you to @Sukera’s post in another thread.

> [@Initialize the julia runtime more than once within C++](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/2):
>
> You only need to initialize the runtime once - it’s supposed to be kept around between function invocations. I suggest you do that outside of your request calling logic, since calling jl\_init more than once effectively means running julia multiple times at the same time (in the same process - which will break).

---

<div class="post-metadata">

**Author:** ![lionisxn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionisxn/32/37989_2.png) [@lionisxn](https://discourse.julialang.org/u/lionisxn)\
**Post date:** [July 25, 2022, 1:24am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/49 "2022-07-25T01:24:31Z")

</div>

I think that it would be better to both call and release a Julia shared library as required, otherwise, to add a checking function on available Julia threads before budgeting new ones. The later manner might be a workaround to avoid unnecessarily activating new threads.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 25, 2022, 5:32pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/50 "2022-07-25T17:32:29Z")

</div>

1. Sure, but these are aspirational goals. Here is the current code.

> <https://github.com/JuliaLang/julia/blob/master/src/init.c#L212>

1. Julia does not dynamically launch threads at the moment. They are allocated on startup. They are only shutdown at process exit at the moment.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 26, 2022, 6:27am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/51 "2022-07-26T06:27:08Z")

</div>

Perhaps some of what you would like to accomplish here may be accomplished by StaticCompiler.jl and StaticTools.jl. This would allow a subset of Julia to be compiled into a static function that does not need the Julia runtime and thus does not need initialization or shutdown.

> [@\[ANN\] StaticTools.jl: Enabling StaticCompiler.jl-based compilation of Julia code to standalone native binaries by avoiding GC allocations and \`llvmcall\`-ing all the things](https://discourse.julialang.org/t/ann-statictools-jl-enabling-staticcompiler-jl-based-compilation-of-julia-code-to-standalone-native-binaries-by-avoiding-gc-allocations-and-llvmcall-ing-all-the-things/80398):
>
> Technically this one has been registered for a couple months already, but we never made an announcement, so here goes! [GitHub - brenhinkeller/StaticTools.jl: Enabling StaticCompiler.jl-based compilation of (some) Julia code to standalone native binaries by avoiding GC allocations and llvmcall-ing all the things!](https://github.com/brenhinkeller/StaticTools.jl) I’ve just released version 0.3, which brings a lot of major changes. While it’s still a somewhat experimental package, I think it’s at least to some extent ready for other folks to use,…

---

<div class="post-metadata">

**Author:** ![lionisxn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionisxn/32/37989_2.png) [@lionisxn](https://discourse.julialang.org/u/lionisxn)\
**Post date:** [July 26, 2022, 6:46am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/52 "2022-07-26T06:46:22Z")

</div>

valuable information, I’ll check and compare it with the “runtime” manner. Many thanks!

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [July 26, 2022, 8:22pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/53 "2022-07-26T20:22:32Z")

</div>

> Doesn’t currently work on Windows

😢

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 27, 2022, 6:26am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/54 "2022-07-27T06:26:03Z")

</div>

Windows Subsystem for Linux 2?

[Previous page](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298.md?page=2)
