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.
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
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.
hi, nkitti! Thank your for your guid. The modifications are made as follows:
The folder of “mylib” is firstly copied to D:\
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)”
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.
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.
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?
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?
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.
I just successfully compiled my project by following the example (GitHub - 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!
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.
Yes, but as you can see init_julia calls julia_init from julia.h:
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:
Here is a key excerpt:
If SafeDllSearchMode is enabled, the search order is as follows:
The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
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.