# 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:** 1

<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 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/1 "2022-07-16T03:14:08Z")

</div>

I am trying to create a .dll that can be called by other languages, e.g. MATLAB or C. I just test the example given in “PackageCompiler.jl”, and generate the following file bundles where “fist\_lib\_wy.dll” is the created dll.

 ![微信截图_20220716110544](https://global.discourse-cdn.com/julialang/original/3X/9/1/9146e59f99739a43e8ff52036baeb489a1b0d40a.png)  
When I tried to call for “fist\_lib\_wy.dll” through the following codes in a script:

const libnm = “D:/MyLib/MyLibCompiled/bin/fist\_lib\_wy”

ccall((:increment64, libnm), Clong, (Clong, ), 1)

The following crash errors emerge:  
Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.  
Exception: EXCEPTION\_ACCESS\_VIOLATION at 0x0 – unknown function (ip: 0000000000000000)  
in expression starting at D:\瀛︿範璧勬枡\endnote鏂  
囦欢搴揬绗旇\璁烘枃鍖匼1 鍒濈鏈姇\ACDCpower\DEBUGDATA.jl:25  
unknown function (ip: 0000000000000000)  
increment64 at D:\MyLib\MyLibCompiled\bin\fist\_lib\_wy.DLL (unknown line)  
top-level scope at D:\瀛︿範璧勬枡\endnote鏂囦欢搴揬绗旇\璁烘枃鍖匼1 鍒濈鏈姇\ACDCpower\DEBUGDATA.jl:25  
jl\_toplevel\_eval\_flex at /cygdrive/c/buildbot/worker/package\_win64/build/src\toplevel.c:876  
jl\_toplevel\_eval\_flex at /cygdrive/c/buildbot/worker/package\_win64/build/src\toplevel.c:830  
jl\_toplevel\_eval at /cygdrive/c/buildbot/worker/package\_win64/build/src\toplevel.c:894 [inlined]  
jl\_toplevel\_eval\_in at /cygdrive/c/buildbot/worker/package\_win64/build/src\toplevel.c:944  
eval at .\boot.jl:373 [inlined]  
include\_string at .\loading.jl:1196  
include\_string at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\utils.jl:286 [inlined]  
#218 at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\eval.jl:121  
withpath at D:\Program Files\Julia.julia\packages\CodeTools\VsjEq\src\utils.jl:30  
unknown function (ip: 0000000000f0ac04)  
withpath at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\eval.jl:9  
#217 at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\eval.jl:119  
unknown function (ip: 0000000000f0a3f3)  
with\_logstate at .\logging.jl:511  
with\_logger at .\logging.jl:623 [inlined]  
#216 at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\eval.jl:118 [inlined]  
hideprompt at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\repl.jl:127  
macro expansion at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\eval.jl:117 [inlined]  
macro expansion at D:\Program Files\Julia.julia\packages\Media\ItEPc\src\dynamic.jl:24 [inlined]  
eval at D:\Program Files\Julia.julia\packages\Atom\BEJkS\src\eval.jl:114  
unknown function (ip: 0000000000f043c3)  
jl\_apply at /cygdrive/c/buildbot/worker/package\_win64/build/src\julia.h:1788 [inlined]  
jl\_f\_\_call\_latest at /cygdrive/c/buildbot/worker/package\_win64/build/src\builtins.c:757  
jl\_apply at /cygdrive/c/buildbot/worker/package\_win64/build/src\julia.h:1788 [inlined]  
do\_apply at /cygdrive/c/buildbot/worker/package\_win64/build/src\builtins.c:713#invokelatest#2 at .\essentials.jl:716  
jl\_apply at /cygdrive/c/buildbot/worker/package\_win64/build/src\julia.h:1788 [Starting Julia…

I have no idea what I missed in the compiling or calling process. I am eager for an answer from the Julia community. Many thanks

---

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

</div>

Do you mean this example?  
[https://julialang.github.io/PackageCompiler.jl/stable/libs.html](https://julialang.github.io/PackageCompiler.jl/stable/libs.html)

It looks like you modified it slightly. Can you show us the modifications?

---

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

</div>

I just realized that you are trying to call this from within Julia. I do not think this will work. For that, try loading the library as a system image.

Otherwise, what you did do is meant to be used by a C program outside of Julia.

---

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

</div>

hi, nkitti! Thank your for your guid. The modifications are made as follows:

1. The folder of “mylib” is firstly copied to D:\
2. run the code in a script “create\_library(“D:/MyLib”, “MyLibCompiled”; lib\_name=“fist\_lib\_wy”, header\_files = [“D:/MyLib/build/mylib.h”], force = true, precompile\_execution\_file = [“D:/MyLib/build/generate\_precompile.jl”], precompile\_statements\_file=[“D:/MyLib/build/additional\_precompile.jl”], incremental = false, filter\_stdlibs = true)”
3. execute " const libnm = “D:/MyLib/MyLibCompiled/bin/fist\_lib\_wy”

ccall((:increment64, libnm), Clong, (Clong, ), 1) "

After the step 3), the above errors are thrown and the REPL crashed

---

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

</div>

Do you mean that the shared library created by Julia can not be called by “ccall”, and only the system image can do so? I have tried to call it through MATLAB by executing “calllib”, and it crashed also.

---

<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 16, 2022, 8:08am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/6 "2022-07-16T08:08:26Z")

</div>

> [@lionisxn](#):
>
> I have tried to call it through MATLAB by executing “calllib”, and it crashed also.

Did you initialize Julia before trying to do so?

---

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

</div>

Take a look at [libcg/main.c at master · simonbyrne/libcg · GitHub](https://github.com/simonbyrne/libcg/blob/master/main-c/main.c)

---

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

</div>

I tried to initial julia using the syntax: calllib(‘fist\_lib\_wy’, ‘init\_julia’, int32(4), {‘’}); However，matlab (ver. 2021b) crashed also. Could you please post a tutorial on how to call the examplified shared library in the binary of PackageCompiler through MATLAB? It will be great helpful to fully understand the integration of Julia with other platform.

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [July 16, 2022, 9:17am UTC](https://discourse.julialang.org/t/is-it-possible-for-ccall-to-call-the-shared-library-created-by-packagecompiler-jl/84298/9 "2022-07-16T09:17:08Z")

</div>

Currently, calling the shared libraries generated by PackageCompiler.jl from Julia does not work. I’m also looking forward to this.

---

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

</div>

> [@mkitti](#):
>
> an you show

I just thought that a shared library created by Julia might be a standard one that can be called for by any language which supports c shared library. Am I wrong?

---

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

</div>

Any solution to this? I’m having the same problem.

---

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

</div>

For instance if I want to call this from Matlab I have to load the library via `loadlibrary`. I can load the `mylib` and find `increment32` and `increment64` in it. But I get an error when directly calling them. As I understand I have to init Julia before calling, but where do I find the `init_julia` and `shudown_julia`?

```matlab
>> addpath('<path>\MyLibCompiled\bin')
>> loadlibrary('mylib', '<path>\MyLibCompiled\include\mylib.h')
>> libfunctions('mylib')

Functions in library mylib:

increment32 increment64  

```

This crashes Matlab

```matlab
>> calllib('mylib', 'increment64', 1)

Stack Trace (from fault):
[0] 0x0000000004cb370f <path>\MyLibCompiled\bin\libjulia-internal.dll+00931599 jl_exception_handler+00000031
[1] 0x0000000004d672e2 <path>\MyLibCompiled\bin\libjulia-internal.dll+01667810 _julia_personality+00000034
[2] 0x00007ffedfef229f C:\WINDOWS\SYSTEM32\ntdll.dll+00664223 _chkstk+00000287
[3] 0x00007ffedfea1454 C:\WINDOWS\SYSTEM32\ntdll.dll+00332884 RtlRaiseException+00001076
[4] 0x00007ffedfef0dce C:\WINDOWS\SYSTEM32\ntdll.dll+00658894 KiUserExceptionDispatcher+00000046

```

---

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

</div>

It is not in `mylib`:

```matlab
>> loadlibrary('mylib', '<path>\MyLibCompiled\include\mylib.h', 'addheader', '<path>\MyLibCompiled\include\init_julia.h')
>> calllib('mylib', 'init_julia', 0, [''])
Error using calllib
Method was not found.

```

---

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

</div>

You still need the Julia runtime, meaning you need the various Julia shared libraries in addition to the one you created. For example, julia.dll or libjulia.so.1.7.

---

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

</div>

What do you mean I need those? `PackageCompiler.jl` bundled them in the bin folder

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

BTW: I’m on Windows

---

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

</div>

`init_julia` is also not from Julia itself but from ‘PackageCompiler.jl’

> <https://github.com/JuliaLang/PackageCompiler.jl/blob/d902a236a3d90bdcb5867967d73df4fd259584f1/src/julia_init.c#L64>

---

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

</div>

I guess `PackageCompiler.jl` compiles the two methods together with the `@ccallable` functions into `mylib`.

> <https://github.com/JuliaLang/PackageCompiler.jl/blob/d902a236a3d90bdcb5867967d73df4fd259584f1/src/PackageCompiler.jl#L522>

But why does Matlab complain than?

---

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

</div>

I just successfully compiled my project by following the example ([GitHub - simonbyrne/libcg](https://github.com/simonbyrne/libcg)). A lot of time was wasted since I was not familiar with the makefile, or essentially, the ‘gcc’ command. After this, I recognized that a julia shared library can not be called without the support of Julia engine, that is why the init\_julia() must be called prior to any shared function. So, I have an idea to make julia shared libary callable by matlab or another language other than C. That is we can compile a julia shared library to a C standard shared library via “gcc”, and edit a head file using C or C++. I am ready to test it, wish good luck!

---

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

</div>

It seems that MATLAB can not directly call a Julia shared library. But I am not sure about this. You can explore it further. I can share a reminder that never forget to add the share/julia/bin to the PATH environment variable if you were using Windows. I’m looking forward to your good news.

---

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

</div>

> [@ohmsweetohm1](#):
>
> `init_julia` is also not from Julia itself but from ‘PackageCompiler.jl’
> 
> [https://github.com/JuliaLang/PackageCompiler.jl/blob/d902a236a3d90bdcb5867967d73df4fd259584f1/src/julia\_init.c#L64](https://github.com/JuliaLang/PackageCompiler.jl/blob/d902a236a3d90bdcb5867967d73df4fd259584f1/src/julia_init.c#L64)

Yes, but as you can see `init_julia` calls `julia_init` from `julia.h`:

> <https://github.com/JuliaLang/julia/blob/6008cbc019424a53786552a511bbef92099c033a/src/julia.h#L1730>

Binary dependency searches are a bit strange on Windows. This is why the DLLs are in the `bin` directory rather than a `lib` directory like on the other operating systems. However, the search process is also well documented:

> **[Dynamic-link library search order - Win32 apps](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order)**
>
> You can control the specific location from which any given DLL is loaded by specifying a full path. If you don't use that method, then the system searches for the DLL at load time as described in this topic.

Here is a key excerpt:

If **SafeDllSearchMode** is enabled, the search order is as follows:

> 1. The directory from which the application loaded.
> 2. The system directory. Use the [**GetSystemDirectory**](https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemdirectorya) function to get the path of this directory.
> 3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
> 4. The Windows directory. Use the [**GetWindowsDirectory**](https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getwindowsdirectorya) function to get the path of this directory.
> 5. The current directory.
> 6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the **App Paths** registry key. The **App Paths** key is not used when computing the DLL search path.

On Windows, Julia itself uses option #1 above. The library and the executables are located in the same `bin` directory. When you launch MATLAB, it is not clear if Julia’s bin directory is either the “The current directory” for option #5 or on the `PATH` for option #6.

Note that the path relative to the library being loaded is not one the standard search path.

One approach on Windows is that you can explicitly load the dependencies first before loading the library of interest. This takes advantage of the following feature:

> If a DLL with the same module name is already loaded in memory, the system checks only for redirection and a manifest before resolving to the loaded DLL, no matter which directory it is in. The system does not search for the DLL.

Given the number of shared libraries that need to be loaded, this might become very tedious. Thus, I recommend check the `PATH` environment variable within MATLAB to make sure the relevant directory is on it. Alternatively, change MATLAB’s current working directory to the relative path with `cd`.

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