# 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:** 20\
**Page:** 2

<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 21, 2022, 12:37am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/21 "2022-07-21T00:37:13Z")

</div>

On Windows, I highly recommend the Dependencies utility to help diagnose shared library issues. The utility will help you see what functions are exported and imported.

> **[GitHub - lucasg/Dependencies: A rewrite of the old legacy software...](https://github.com/lucasg/Dependencies)**
>
> A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues. - GitHub - lucasg/Dependencies: A rewrite of the old legacy softwar...

Also if you have the Visual Studio community edition download you can you use the `dumpbin.exe` utility:

> **[DUMPBIN Reference](https://learn.microsoft.com/en-us/cpp/build/reference/dumpbin-reference?view=msvc-170)**
>
> Learn more about: DUMPBIN Reference

---

<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, 4:49am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/22 "2022-07-21T04:49:55Z")

</div>

As you can see from my original post, I have added the `bin` directory to Matlab and it seems to have no trouble finding stuff. This can also be seen in the stacktrace, were the error is in `libjulia-internal`. I think it is just because I did not call `init_julia` first. However I’m not sure how to do this and where to find the method.

---

<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, 10:06am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/23 "2022-07-21T10:06:27Z")

</div>

If checked `mylib.dll` with `DUMPBIN` and the `init_julia` and `shutdown_julia` are in there.

---

<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 21, 2022, 10:07am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/24 "2022-07-21T10:07:22Z")

</div>

> [@ohmsweetohm1](#):
>
> As you can see from my original post, I have added the `bin` directory to Matlab and it seems to have no trouble finding stuff

It appears that you added the `bin` directory to the _MATLAB_ path, but it is not clear to me that you added it to the _Windows_ PATH environment variable or changed the current working directory to the `bin` directory. You do have a good point in that the stack trace seems to have located `libjulia-internal.dll`.

What I have not seen is an attempt to load `libjulia.dll` and list the available symbols there. `libjulia.dll` should export the symbol `julia_init` (1.7) or `julia_init__threading` (1.6).

To be clear, the challenge here is that you are trying to embed Julia in MATLAB. This means that you need to implement the C code in [`julia_init.c`](https://github.com/JuliaLang/PackageCompiler.jl/blob/v2.0.7/src/julia_init.c) in MATLAB or modify this example so that those symbols are exported so that you can call them from MATLAB.

---

<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 21, 2022, 10:17am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/25 "2022-07-21T10:17:38Z")

</div>

Let’s try a simple experiment first. Let’s put the package compiler stuff aside for a second and just embed Julia in MATLAB. I’m loosely referencing the embedding guide here.

[https://docs.julialang.org/en/v1/manual/embedding/](https://docs.julialang.org/en/v1/manual/embedding/)

Whatever preprocessor that MATLAB’s `loadlibrary` is using to process headers seems to be unable to process `julia.h` directly. So let’s create a simpler header called `julia_for_matlab.h`:

```nohighlight
void jl_init(void);
void jl_init_with_image(const char *julia_bindir, const char *image_path);
void* jl_eval_string(const char *str);
double jl_unbox_float64(void *v);
void jl_atexit_hook(int status);

```

For simplicity, I just threw this into the `bin` folder.

Then in MATLAB, we execute the following:

```nohighlight
>> cd("C:\dog\run\path\to\julia\bin")
>> loadlibrary("libjulia.dll", "julia_for_matlab.h")
>> calllib("libjulia", "jl_init")
>> ptr = calllib("libjulia", "jl_eval_string", 'sqrt(25.0)')

ptr =

libpointer
>> calllib("libjulia", "jl_unbox_float64", ptr)

ans =

     5

>> ptr = calllib("libjulia", "jl_eval_string", 'Float64(pi)')

ptr =

libpointer
>> calllib("libjulia", "jl_unbox_float64", ptr)

ans =

    3.1416

```

We appear to have successfully embedded Julia within MATLAB 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 21, 2022, 10:26am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/26 "2022-07-21T10:26:54Z")

</div>

Now you might be getting confused because we were talking about `julia_init` earlier and now I’ve invoked `jl_init` as mentioned in the embedding manual.

`jl_init` and `jl_init_with_image` are implemented as follows.

> <https://github.com/JuliaLang/julia/blob/762561c3057b4bd541d183ca1d1b70b9945bff22/src/jlapi.c#L55-L84>

On Line 66, `julia_init` is invoked.

A more advanced form, specifying a custom system image may be invoked as follows:

```nohighlight
>> loadlibrary("libjulia.dll", "julia_for_matlab.h")
>> calllib("libjulia", "jl_init_with_image", pwd(), fullfile(pwd(), '..\lib\julia\sys.dll'))
>> ptr = calllib("libjulia", "jl_eval_string", 'sum(pi .+ (1:5))')

ptr =

libpointer
>> calllib("libjulia", "jl_unbox_float64", ptr)

ans =

   30.7080

```

From here, you should now be able to initialize Julia with your custom image specified via the second argument of `jl_init_with_image`. You should be able then invoke your function via `calllib` and `jl_eval_string` or perhaps calling the C function pointer directly.

---

<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, 11:28am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/27 "2022-07-21T11:28:25Z")

</div>

I have talked about `init_julia` from `PackageCompiler.jl` and not `julia_init`.  
The example from the docs states to calling it.

> <https://github.com/simonbyrne/libcg/blob/f89357391fe318c9ffa9166ff02bda814f8d908a/main-c/main.c#L25>

---

<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, 11:39am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/28 "2022-07-21T11:39:17Z")

</div>

I have also tried a VS C++ console application.

```cpp
#include <iostream>
#include <julia_init.h>
#include <mylib.h>

int main(int argc, char *argv[])
{
    init_julia(argc, argv);
    int32_t u = 1;
    u = increment32(u);
    std::cout << "Output from Julia:" << u << std::endl;
    shutdown_julia(0);
    return 0;
}

```

But I’m not able to compile it:

```shell
1>ConsoleApplication2.cpp
1>ConsoleApplication2.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl init_julia(int,char * * const)" (?init_julia@@YAXHQEAPEAD@Z)" in Funktion "main".
1>ConsoleApplication2.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""void __cdecl shutdown_julia(int)" (?shutdown_julia@@YAXH@Z)" in Funktion "main".
1>ConsoleApplication2.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""int __cdecl increment32(int)" (?increment32@@YAHH@Z)" in Funktion "main".
1><path>\ConsoleApplication2\x64\Debug\ConsoleApplication2.exe : fatal error LNK1120: 3 nicht aufgelöste Externe
1>Die Erstellung des Projekts "ConsoleApplication2.vcxproj" ist abgeschlossen -- FEHLER.

```

I have added the `include` dir to the `C/C++|General` `Additional Include Directories` and the the `bin` dir to `Linker|General` `Additional Library Directories`.

Sry, for the log being in German.

---

<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 21, 2022, 2:01pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/29 "2022-07-21T14:01:42Z")

</div>

As far as I can tell, `init_julia` may have not been exported. If you want to dynamically link to the library, the symbol needs to be exported.

Try modifying `init_julia.h` as follows:

```julia
JL_DLLEXPORT void init_julia(int argc, char *argv[]);
JL_DLLEXPORT void shutdown_julia(int retcode);

```

Also add `JL_DLLEXPORT` to `init_julia.c`. The definition of `JL_DLLEXPORT` should come from `julia.h`.

From the “x64 native tools command prompt for visual studio” what does `dumpbin` report?

```julia
dumpbin /exports mylib.dll

```

---

<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, 2:07pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/30 "2022-07-21T14:07:56Z")

</div>

```shell
File Type: DLL

  Section contains the following exports for mylib.dll

    00000000 characteristics
    62D824E8 time date stamp Wed Jul 20 17:53:12 2022
        0.00 version
           1 ordinal base
          21 number of functions
          21 number of names

    ordinal hint RVA name

          1 0 002EAD00 get_sysimage_path
          2 1 00001470 increment32
          3 2 000013B0 increment64
          4 3 002EADF0 init_julia
          5 4 039AFB58 jl_RTLD_DEFAULT_handle_pointer
          6 5 039BEB68 jl_dispatch_fvars_idxs
          7 6 039BEB6C jl_dispatch_fvars_offsets
          8 7 039BEB64 jl_dispatch_reloc_slots
          9 8 039BEAF0 jl_dispatch_target_ids
         10 9 03A32620 jl_pgcstack_func_slot
         11 A 03A32628 jl_pgcstack_key_slot
         12 B 00001530 jl_sysimg_fvars_base
         13 C 039AFB60 jl_sysimg_fvars_offsets
         14 D 03A271A8 jl_sysimg_gvars_base
         15 E 039B95D0 jl_sysimg_gvars_offsets
         16 F 002EE940 jl_system_image_data
         17 10 039BEBD8 jl_system_image_size
         18 11 03A32630 jl_tls_offset
         19 12 002EAD70 set_depot_load_path
         20 13 002EACD0 setup_args
         21 14 002EAE70 shutdown_julia

  Summary

        1000 .CRT
        C000 .bss
     36BC000 .data
        1000 .debug_abbrev
        1000 .debug_aranges
        1000 .debug_frame
      24B000 .debug_info
      142000 .debug_line
      174000 .debug_ranges
       2A000 .debug_str
        1000 .edata
        2000 .idata
       1F000 .pdata
       16000 .rdata
        1000 .reloc
      2ED000 .text
        1000 .tls
       48000 .xdata

```

---

<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 21, 2022, 2:32pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/31 "2022-07-21T14:32:18Z")

</div>

What does `mylib.h` look like? Does it include “init\_julia.h”?

```C
#include "init_julia.h"

```

MATLAB’s `addheader` parameter requires that

> Each file specified by `addheader` must have a corresponding `#include` statement in the base header file.

> **[Load C shared library into MATLAB - MATLAB loadlibrary](https://www.mathworks.com/help/matlab/ref/loadlibrary.html#namevaluepairarguments)**
>
> This MATLAB function loads functions from C shared library libname defined in header file hfile into MATLAB.

For Visual C++, have you generated .lib files?

> <https://stackoverflow.com/questions/9360280/how-to-make-a-lib-file-when-have-a-dll-file-and-a-header-file>

---

<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, 2:46pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/32 "2022-07-21T14:46:21Z")

</div>

```cpp
#include <init_julia.h>

int increment32(int);
long increment64(long);

```

```matlab
addpath('<path>\MyLibCompiled\bin')

loadlibrary('mylib',...
    '<path>\MyLibCompiled\include\mylib.h',...
    'addheader', '<path>\MyLibCompiled\include\julia_init.h')

libfunctions('mylib')
unloadlibrary('mylib')

```

```matlab
Error using loadlibrary
Failed to preprocess the input file.
 Output from preprocessor is:<path>\MyLibCompiled\include\mylib.h:1:24:
 fatal error: init_julia.h: No such file or directory
 #include <init_julia.h>
                        ^
compilation terminated.

Error in test (line 3)
loadlibrary('mylib',...

```

---

<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, 2:54pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/33 "2022-07-21T14:54:10Z")

</div>

I do not see why I would need to make a `.lib`.  
The embedding Julia example did work - also not straight away, but at least after I read through some open [issues](https://github.com/JuliaLang/julia/issues/34201):

```cpp
#include <uv.h>
#include <windows.h>
#include <julia.h>

int main(int argc, char* argv[])
{
    jl_init();
    jl_eval_string("print(sqrt(2.0))");
    jl_atexit_hook(0);
    return 0;
}

```

The project dependecies are setup as is explained [here](https://docs.julialang.org/en/v1/manual/embedding/#High-Level-Embedding-on-Windows-with-Visual-Studio).

---

<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, 3:11pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/34 "2022-07-21T15:11:02Z")

</div>

Altering the `mylib.h` worked:

## `mylib.h`:

```cpp
void init_julia(int argc, char *argv[]);
void shutdown_julia(int retcode);
int increment32(int);
long increment64(long);

```

## `test.m`:

```matlab
addpath('<path>\MyLibCompiled\bin')

if not(libisloaded('mylib'))
    loadlibrary('mylib',...
        '<path>\MyLibCompiled\include\mylib.h')
end

libfunctions('mylib')

calllib('mylib', 'init_julia', 0, {''})
i = 1;
for k = 1:10
    i = calllib('mylib', 'increment32', i);
    disp(i)
end
calllib('mylib', 'shutdown_julia', 0)

unloadlibrary('mylib')

```

```matlab
>> test

Functions in library mylib:

increment32 increment64 init_julia shutdown_julia  

     2

     3

     4

     5

     6

     7

     8

     9

    10

    11

```

Running this Matlab script a second time crashes Matlab, however.

```shell

Abnormal termination:
abort()

Register State (captured):
  RAX = 00007ffd61cc19a8 RBX = 00007ffd61cc19a8
  RCX = 000000963cbf3ad0 RDX = 0000000000000000
  RSP = 000000963cbf3a50 RBP = 000001f1f83d8360
  RSI = 0000000000000000 RDI = 0000000000000000
 
   R8 = 0000000000000003 R9 = 000000963cbf39b8
  R10 = 0000000000000014 R11 = 00007ffd61cb1947
  R12 = 000000963cbf4708 R13 = 0000000000000000
  R14 = 000000963cbf4140 R15 = 00007ffd61cb18e8
 
  RIP = 00007ffd61a6476a EFL = 00000206
 
   CS = 0033 FS = 0053 GS = 002b

Stack Trace (captured):
[0] 0x00007ffd61a5c9c3 C:\Program Files\MATLAB\R2020a\bin\win64\libmwfl.dll+00051651 foundation::core::diag::thread_context::unspecified_bool+00000051
[1] 0x00007ffd61a598a8 C:\Program Files\MATLAB\R2020a\bin\win64\libmwfl.dll+00039080 foundation::core::diag::stacktrace_base::capture+00000024
[2] 0x00007ffd61a6009b C:\Program Files\MATLAB\R2020a\bin\win64\libmwfl.dll+00065691 foundation::core::diag::symbols::getSymbolAddress+00012859
[3] 0x00007ffd61a63391 C:\Program Files\MATLAB\R2020a\bin\win64\libmwfl.dll+00078737 foundation::core::diag::is_terminate_message_enabled+00000577
[4] 0x00007ffd0d26a50f C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+01156367 mcr::shutdown::startWatchdog+00039423
[5] 0x00007ffd0d269e3c C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+01154620 mcr::shutdown::startWatchdog+00037676
[6] 0x00007ffd0d265473 C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+01135731 mcr::shutdown::startWatchdog+00018787
[7] 0x00007ffd0d267817 C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+01144855 mcr::shutdown::startWatchdog+00027911
[8] 0x00007ffe07a21881 C:\WINDOWS\System32\ucrtbase.dll+00465025 raise+00000481
[9] 0x00007ffe07a22851 C:\WINDOWS\System32\ucrtbase.dll+00469073 abort+00000049
[10] 0x00007ffe07a21f9f C:\WINDOWS\System32\ucrtbase.dll+00466847 terminate+00000031
[11] 0x00007ffcff9eb303 C:\Program Files\MATLAB\R2020a\bin\win64\performance.dll+00897795 Performance::GetNanosecondTimer+00073891
[12] 0x00007ffe079c42d6 C:\WINDOWS\System32\ucrtbase.dll+00082646 execute_onexit_table+00000342
[13] 0x00007ffe079c41fb C:\WINDOWS\System32\ucrtbase.dll+00082427 execute_onexit_table+00000123
[14] 0x00007ffe079c41b4 C:\WINDOWS\System32\ucrtbase.dll+00082356 execute_onexit_table+00000052
[15] 0x00007ffcff9dd422 C:\Program Files\MATLAB\R2020a\bin\win64\performance.dll+00840738 Performance::GetNanosecondTimer+00016834
[16] 0x00007ffcff9dd548 C:\Program Files\MATLAB\R2020a\bin\win64\performance.dll+00841032 Performance::GetNanosecondTimer+00017128
[17] 0x00007ffe0a009a1d C:\WINDOWS\SYSTEM32\ntdll.dll+00104989 RtlActivateActivationContextUnsafeFast+00000285
[18] 0x00007ffe0a04db91 C:\WINDOWS\SYSTEM32\ntdll.dll+00383889 LdrShutdownProcess+00000321
[19] 0x00007ffe0a04da2d C:\WINDOWS\SYSTEM32\ntdll.dll+00383533 RtlExitUserProcess+00000173
[20] 0x00007ffe0840e0ab C:\WINDOWS\System32\KERNEL32.DLL+00123051 FatalExit+00000011
[21] 0x00007ffe09c0a155 C:\WINDOWS\System32\msvcrt.dll+00237909 exit+00000133
[22] 0x00007ffe09c0a7c5 C:\WINDOWS\System32\msvcrt.dll+00239557 initterm_e+00000581
[23] 0x00007ffe09bfae36 C:\WINDOWS\System32\msvcrt.dll+00175670 raise+00000598
[24] 0x0000000004fe358b <path>\MyLibCompiled\bin\libjulia-internal.dll+00931211 jl_critical_error+00000555
[25] 0x00007ffe09bfadfb C:\WINDOWS\System32\msvcrt.dll+00175611 raise+00000539
[26] 0x00007ffe09bff1fb C:\WINDOWS\System32\msvcrt.dll+00193019 abort+00000027
[27] 0x000000000510cf9b <path>\MyLibCompiled\bin\libjulia-internal.dll+02150299 utf8proc_NFKC_Casefold+00233147
[28] 0x0000000004fb8d3c <path>\MyLibCompiled\bin\libjulia-internal.dll+00757052 jl_restore_system_image+00000268
[29] 0x0000000004f9f21d <path>\MyLibCompiled\bin\libjulia-internal.dll+00651805 jl_get_libllvm+00002157
[30] 0x000000000127ae61 <path>\MyLibCompiled\bin\mylib.dll+03059297 init_julia+00000113
[31] 0x00000000653814df C:\Users\kaisv.TUGRAZ\AppData\Local\Temp\tp1c0e807c_04bd_4ff3_854c_b185fb6c1c14\mylib_thunk_pcwin64.dll+00005343 voidint32voidPtrThunk+00000063
[32] 0x00007ffced5ff43d C:\Program Files\MATLAB\R2020a\bin\win64\libmwcli.dll+00062525
[33] 0x00007ffced6038cb C:\Program Files\MATLAB\R2020a\bin\win64\libmwcli.dll+00080075 mwboost::serialization::singleton_module::get_lock+00000555
[34] 0x00007ffced623083 C:\Program Files\MATLAB\R2020a\bin\win64\libmwcli.dll+00209027 PointerMapSize+00102067
[35] 0x00007ffced62453d C:\Program Files\MATLAB\R2020a\bin\win64\libmwcli.dll+00214333 PointerMapSize+00107373
[36] 0x00007ffd0cd5b3bc bin\win64\pgo\m_dispatcher.dll+00046012 Mfh_MATLAB_fn_impl::dispatch_mf_with_reuse+00000172
[37] 0x00007ffd0cd61f12 bin\win64\pgo\m_dispatcher.dll+00073490 Mlm_file::needToCheckoutLicense+00002674
[38] 0x00007ffd0cd6143d bin\win64\pgo\m_dispatcher.dll+00070717 Mfh_MATLAB_fn_impl::dispatch+00000045
[39] 0x00007ffd0a5b3898 bin\win64\pgo\m_lxe.dll+00538776
[40] 0x00007ffd0a57c386 bin\win64\pgo\m_lxe.dll+00312198
[41] 0x00007ffd0a57c270 bin\win64\pgo\m_lxe.dll+00311920
[42] 0x00007ffd0a5b1ce4 bin\win64\pgo\m_lxe.dll+00531684
[43] 0x00007ffd0a6c89b1 bin\win64\pgo\m_lxe.dll+01673649 MathWorks::lxe::ShutdownLxeEngine+00081477
[44] 0x00007ffd0a5a3f8d bin\win64\pgo\m_lxe.dll+00475021
[45] 0x00007ffd0a5a6344 bin\win64\pgo\m_lxe.dll+00484164
[46] 0x00007ffd0a5a745d bin\win64\pgo\m_lxe.dll+00488541
[47] 0x00007ffd0a5a704f bin\win64\pgo\m_lxe.dll+00487503
[48] 0x00007ffd0a6b0fb4 bin\win64\pgo\m_lxe.dll+01576884 MathWorks::lxe::StartupLxeEngine+00374224
[49] 0x00007ffd0a5af30b bin\win64\pgo\m_lxe.dll+00520971
[50] 0x00007ffd0a5a562e bin\win64\pgo\m_lxe.dll+00480814
[51] 0x00007ffd0a5a46ac bin\win64\pgo\m_lxe.dll+00476844
[52] 0x00007ffd0a64c981 bin\win64\pgo\m_lxe.dll+01165697
[53] 0x00007ffd0a5a4490 bin\win64\pgo\m_lxe.dll+00476304
[54] 0x00007ffd0a6ea2ad bin\win64\pgo\m_lxe.dll+01811117 mwboost::archive::detail::oserializer<mwboost::archive::binaryTerm_oarchive,ir::IrTree>::oserializer<mwboost::archive::binaryTerm_oarchive,ir::IrTree>+00056793
[55] 0x00007ffd0cdeaebd bin\win64\pgo\m_dispatcher.dll+00634557 Mfh_file::dispatch_file_common+00000165
[56] 0x00007ffd0cdea54d bin\win64\pgo\m_dispatcher.dll+00632141 Mfh_error::lookup+00006517
[57] 0x00007ffd0cdeae11 bin\win64\pgo\m_dispatcher.dll+00634385 Mfh_file::dispatch+00000045
[58] 0x00007ffd0a5b3898 bin\win64\pgo\m_lxe.dll+00538776
[59] 0x00007ffd0a57c386 bin\win64\pgo\m_lxe.dll+00312198
[60] 0x00007ffd0a57c270 bin\win64\pgo\m_lxe.dll+00311920
[61] 0x00007ffd0a5b1ce4 bin\win64\pgo\m_lxe.dll+00531684
[62] 0x00007ffd0a6c89b1 bin\win64\pgo\m_lxe.dll+01673649 MathWorks::lxe::ShutdownLxeEngine+00081477
[63] 0x00007ffd0a5a3f8d bin\win64\pgo\m_lxe.dll+00475021
[64] 0x00007ffd0a5a6344 bin\win64\pgo\m_lxe.dll+00484164
[65] 0x00007ffd0a5a745d bin\win64\pgo\m_lxe.dll+00488541
[66] 0x00007ffd0a5a704f bin\win64\pgo\m_lxe.dll+00487503
[67] 0x00007ffd0a6b0fb4 bin\win64\pgo\m_lxe.dll+01576884 MathWorks::lxe::StartupLxeEngine+00374224
[68] 0x00007ffd0a5af30b bin\win64\pgo\m_lxe.dll+00520971
[69] 0x00007ffd0a5a562e bin\win64\pgo\m_lxe.dll+00480814
[70] 0x00007ffd0a5a46ac bin\win64\pgo\m_lxe.dll+00476844
[71] 0x00007ffd0a615abf bin\win64\pgo\m_lxe.dll+00940735
[72] 0x00007ffd0a615c2b bin\win64\pgo\m_lxe.dll+00941099
[73] 0x00007ffd0a615b22 bin\win64\pgo\m_lxe.dll+00940834
[74] 0x00007ffd0a64ce70 bin\win64\pgo\m_lxe.dll+01166960
[75] 0x00007ffd0a64ce1e bin\win64\pgo\m_lxe.dll+01166878
[76] 0x00007ffd0c064ef8 bin\win64\pgo\m_lxe.dll+28528376 MathWorks::lxe::profiler::IMexProfilingSupport::~IMexProfilingSupport+00083312
[77] 0x00007ffd0d0fda8c C:\Program Files\MATLAB\R2020a\bin\win64\libmwbridge.dll+00187020 mnParser+00001212
[78] 0x00007ffd0d1d6a99 C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00551577 mcr_set_enableReadingFromStdin+00016745
[79] 0x00007ffd0d16a05a C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00106586 mcrOptions::set_use_license_manager+00071962
[80] 0x00007ffd0d18b04a C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00241738 mcrOptions::set_use_license_manager+00207114
[81] 0x00007ffd0d40653e C:\Program Files\MATLAB\R2020a\bin\win64\iqm.dll+00615742 iqm::PackagedTaskPlugin::execute+00000862
[82] 0x00007ffd0d4063c9 C:\Program Files\MATLAB\R2020a\bin\win64\iqm.dll+00615369 iqm::PackagedTaskPlugin::execute+00000489
[83] 0x00007ffd0d1d084e C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00526414 mcrIsExternalAvailableBuiltin+00045518
[84] 0x00007ffd0d3dca03 C:\Program Files\MATLAB\R2020a\bin\win64\iqm.dll+00444931 iqm::Iqm::setupIqmFcnPtrs+00100227
[85] 0x00007ffd0d3b4e2f C:\Program Files\MATLAB\R2020a\bin\win64\iqm.dll+00282159 iqm::Iqm::create+00009279
[86] 0x00007ffd0d3b4428 C:\Program Files\MATLAB\R2020a\bin\win64\iqm.dll+00279592 iqm::Iqm::create+00006712
[87] 0x00007ffd0d1a7d06 C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00359686 mcrInstantiationError::operator=+00012150
[88] 0x00007ffd0d1a830c C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00361228 mcrInstantiationError::operator=+00013692
[89] 0x00007ffd0d1a5f0c C:\Program Files\MATLAB\R2020a\bin\win64\mcr.dll+00352012 mcrInstantiationError::operator=+00004476
[90] 0x00007ffd9f8ea203 C:\Program Files\MATLAB\R2020a\bin\win64\mwboost_thread-vc141-mt-x64-1_70.dll+00041475 mwboost::thread::swap+00000083
[91] 0x00007ffe079d1bb2 C:\WINDOWS\System32\ucrtbase.dll+00138162 configthreadlocale+00000146
[92] 0x00007ffe08407034 C:\WINDOWS\System32\KERNEL32.DLL+00094260 BaseThreadInitThunk+00000020
[93] 0x00007ffe0a042651 C:\WINDOWS\SYSTEM32\ntdll.dll+00337489 RtlUserThreadStart+00000033

```

---

<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 21, 2022, 3:29pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/35 "2022-07-21T15:29:13Z")

</div>

You might not want to call `calllib('mylib', 'shutdown_julia', 0)` unless you are going to shutdown 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 21, 2022, 3:41pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/36 "2022-07-21T15:41:21Z")

</div>

Why not?

I was able to load the library again and call `libfunctions`. However, the second `init_julia` call produces the error. I guess something is not properly closed in the first `shutdown_julia`.

---

<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 21, 2022, 3:46pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/37 "2022-07-21T15:46:24Z")

</div>

As far as I know, `jl_atexit_hook` is only intended to be called exactly once within a process. It is not clear to me if Julia is able to reinitialize itself properly after that.

---

<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, 3:49pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/38 "2022-07-21T15:49:49Z")

</div>

It seems not.

The failure is somehow related to `jl_restore_system_image`

The docs state the following:

> If there is a sysimg file, it contains a pre-cooked image of the Core and Main modules (and whatever else is created by boot.jl). See Building the Julia system image.

> jl\_restore\_system\_image() deserializes the saved sysimg into the current Julia runtime environment and initialization continues after jl\_init\_box\_caches() below…

---

<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 21, 2022, 3:53pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/39 "2022-07-21T15:53:56Z")

</div>

> [@ohmsweetohm1](#):
>
> I have added the `include` dir to the `C/C++|General` `Additional Include Directories` and the the `bin` dir to `Linker|General` `Additional Library Directories`.
> 
> Sry, for the log being in German.

Make sure to follow all the steps

1. Set the `JULIA_DIR` environment variable. That should be the folder that contains `bin`, `include`, and `lib`.
2. The `bin` folder under JULIA\_DIR should be on the system PATH.
3. To modify the project, I used this main function and needed to add two additional preprocessor directives.

```cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <julia.h>

int main()
{
    jl_init();
    jl_eval_string("print(sqrt(2.0))");
    jl_atexit_hook(0);
    return 0;
}

```

1. Set the project to Debug x64
2. Using the project Properties dialog, go to `C/C++` | `General` and add `$(JULIA_DIR)\include\julia\` to the Additional Include Directories property
3. Then, go to the `Linker` | `General` section and add `$(JULIA_DIR)\lib` to the Additional Library Directories property.
4. Finally, under `Linker` | `Input` , add `libjulia.dll.a;libopenlibm.dll.a;` to the list of libraries

I then get the following build log:

```julia
Build started...
1>------ Build started: Project: JuliaDemoEmbedding, Configuration: Debug x64 ------
1>JuliaDemoEmbedding.cpp
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt\signal.h(26,1): warning C4005: 'NSIG': macro redefinition
1>~\.julia\juliaup\julia-1.7.3+0~x64\include\julia\uv\win.h(75): message : see previous definition of 'NSIG'
1>JuliaDemoEmbedding.vcxproj -> ~\source\repos\JuliaDemoEmbedding\x64\Debug\JuliaDemoEmbedding.exe
1>Done building project "JuliaDemoEmbedding.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

```

---

<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 21, 2022, 3:58pm UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/40 "2022-07-21T15:58:04Z")

</div>

> [@ohmsweetohm1](#):
>
> I do not see why I would need to make a `.lib`.

This is very particular to Visual Studio C++. In the embedding example above, the “libjulia.dll.a;libopenlibm.dll.a” serves this purpose.

See this SO article for details:

> <https://stackoverflow.com/questions/56085753/why-do-we-need-lib-file-in-case-of-importing-functions-from-dll>

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

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