# How correct open and close Julia DLL in C++?

**URL:** <https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877>\
**Category:** Internals & Design\
**Tags:** question, windows, embedding\
**Created:** [May 23, 2017, 1:28pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877 "2017-05-23T13:28:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Florek](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Martin\_Florek](https://discourse.julialang.org/u/Martin_Florek)\
**Post date:** [May 23, 2017, 1:28pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/1 "2017-05-23T13:28:52Z")

</div>

When I compiled Julie with my own `userimg.jl` sometimes the compile process does not create correct the DLL (System Image Building with help of `createDLL.bat`). Here’s the key command.

> %Pathname%\julia.exe “%workDir%build\_sysimg\_tim.jl” libXJZ core2 “%workDir%userimg.jl” --force

Sometimes, after compilation I can not open the final `libXJZ_tim.dll` in C/C++ with the help of `julia.h` [(Embedding Julia)](https://docs.julialang.org/en/stable/manual/embedding/). I get error in function `jl_init_with_image()` respectively program only crash ☹ .  
Here is the print screen

> **[Google Drive: Sign-in](https://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D0ByRuhkKbXjteMjctVk5MQ19qRFE&followup=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D0ByRuhkKbXjteMjctVk5MQ19qRFE&ifkv=ASKXGp0FM7vrzE2C5ouZtDI4miEiVvLiV3QM8xZdbw06qo3wwdtqKSc24-kaXJuMBkrzUo0faFyp0A&osid=1&passive=1209600&service=wise&flowName=GlifWebSignIn&flowEntry=ServiceLogin&dsh=S1024159932%3A1701593763409911&theme=glif)**
>
> Access Google Drive with a Google account (for personal use) or Google Workspace account (for business use).

 ![](https://global.discourse-cdn.com/julialang/original/3X/b/8/b8928170bbf47bd6012e7e533ea22fa7a5f917ef.png)

I also created mini statistics good/ bad open DLLs (Of course without changing the code, I just called the compilation script).  
 ![](https://global.discourse-cdn.com/julialang/original/3X/0/9/098b541fcb705320f03dd9771f651f8bd556c451.png)

I created a simple demo in Visual Studio with function `foo()` to control the DLL opening. Function `jl_init_with_image()` calls `julia_init()` where program crash. I modify `jl_init_with_image` and created own version to detect the problem.

My questions are:  
**I.** How to exit the program correctly when the function ` julia_init()` does not return anything and the program only crash?  
(How to capture an exception?,…?)

**II.** How to ensure stable compilation for system image building to work every created dll?

All the necessary files with **good** / **bad** DLL and Project Visual Studio are in [Drive](https://drive.google.com/open?id=0ByRuhkKbXjteX2RjTkE0UFB2T2s).  
This topic is closely related [“Static julia compilation with own package”](https://discourse.julialang.org/t/static-julia-compilation-with-own-package/1435)

INFO:  
Microsoft Visual Studio Community 2015, Version: 14.0.  
On Windows 8.1

> julia\> versioninfo()  
> Julia Version 0.5.1  
> Commit 6445c82 (2017-03-05 13:25 UTC)  
> Platform Info:  
> OS: Windows (x86\_64-w64-mingw32)  
> CPU: Intel(R) Xeon(R) CPU E5-2430L v2 @ 2.40GHz  
> WORD\_SIZE: 64  
> BLAS: libopenblas (USE64BITINT DYNAMIC\_ARCH NO\_AFFINITY Sandybridge)  
> LAPACK: libopenblas64\_  
> LIBM: libopenlibm  
> LLVM: libLLVM-3.7.1(ORCJIT, ivybridge)

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [May 24, 2017, 2:04pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/2 "2017-05-24T14:04:12Z")

</div>

As mentioned in the other thread, please start from the [embedding example](https://github.com/JuliaLang/julia/blob/master/examples/embedding/embedding.c)!

I’m not going to debug your code at the moment, but my main comment is that in 0.6 the **only** call you should need is `jl_init()` (on 0.5 you need to provide the image path). You should not need to call `libsupport_init` yourself, and it’s not clear why so much other initialization code is duplicated. Again, start from the embedding example and the default system image: make sure you can compile it with MSVC, then work from there.

I’m not sure I understand the first question: if the program crashes, it crashes… [edit: the runtime doesn’t exist at that point, so there’s no Julia-level exception to capture – the debugger is the only option then]

For the second question: if you can isolate a build issue in a clean way (using the recommended initialization functions), then please file a bug and hopefully someone can take a look. However, we are now exercising the embedding code on Windows (and elsewhere) with every Continuous Integration (appveyor) run, so hopefully such an issue would have been seen already. Ref:

[https://github.com/JuliaLang/julia/pull/21299](https://github.com/JuliaLang/julia/pull/21299)

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [May 25, 2017, 7:58am UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/3 "2017-05-25T07:58:10Z")

</div>

> I’m not sure I understand the first question: if the program crashes, it crashes…

The problem is that the program crashes, and doesn’t treat the crash itself - there is no error returned. `jl_init_with_image` doesn’t return anything. (so we cannot correctly close the process which calls `jl_init_with_image`)

> For the second question…

The problems can be following:

- There is no error in Julia code, but some names for example variable `e` conflicts. This produces warning at least, but the compiled DLL may or may not crash - undefined behavior in other words!
- The build process (compiling and linking Julia code into DLL) finishes without any error or warning. But the code doesn’t run as expected. After a long and desperate debugging one finds and error in Julia code (might be simple typo). I compare it to C++ as an example: compiler checks each and every character of the code, and once compiled, I’m sure the code is correct (I mean the code syntactically itself). In Julia until we run the code, we don’t know if we have correct code. Of course we can write a unit test for each and every line of code… I personally like what Stroustrup said in C++ book: don’t try to be smarter than compiler and use compiler as at tool for finding errors.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 25, 2017, 8:56am UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/4 "2017-05-25T08:56:42Z")

</div>

Are you trying to say that you like static languages more than dynamic languages?

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [May 25, 2017, 9:21am UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/5 "2017-05-25T09:21:34Z")

</div>

I don’t try to say what I like more 🙂  
I’m describing everyday problems, that we’re facing, when trying to use Julia for product development. And since I’ve been using C++ for 10+ years (in MS VS), it comes to me natural to compare it. I started with Julia a year ago.

Julia is great math library.

And it is hard to compare languages itself. Great part of a ‘feeling’ comes for me from development environment. We all know work in Visual Studio is great.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [May 25, 2017, 1:22pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/6 "2017-05-25T13:22:07Z")

</div>

As far as runtime errors, for the most part, that’s a known design trade-off when choosing to use Julia.

In general, I would suggest you get familiar with and use GDB to debug initialization issues, because debugging and backtraces should make more sense there than in MSVC.

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [May 25, 2017, 1:36pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/7 "2017-05-25T13:36:20Z")

</div>

Thanks for suggesting GDB.  
I don’t use MSVS for debugging Julia lib (what doesn’t make sense as you pointed out). I just mean debugging native C++ code is great there.

---

<div class="post-metadata">

**Author:** ![goldfita](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@goldfita](https://discourse.julialang.org/u/goldfita)\
**Post date:** [July 14, 2017, 6:32pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/8 "2017-07-14T18:32:55Z")

</div>

I also get a stackoverflow when trying to embed Julia-0.6.1-pre.0 in C#. Issue [#22320](https://github.com/JuliaLang/julia/issues/22320) seems like it might be related. I tried increasing the stack size of the .Net executable, but it didn’t help.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [July 14, 2017, 7:45pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/9 "2017-07-14T19:45:04Z")

</div>

Yes, that’s exactly the issue. Unfortunately I’m not sure what, if anything, can be done about that in the short term.

As far as the work-around, could you please double-check against 0.6 release? I have used 8MB stack size with C# and C++ on VS2015 against some pre-release or late nightly in the 0.6 series, I’m not sure the date exactly, and it worked in simple initialization and call tests (the specific problem is too-deep recursion while reading back the system image).

---

<div class="post-metadata">

**Author:** ![goldfita](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@goldfita](https://discourse.julialang.org/u/goldfita)\
**Post date:** [July 14, 2017, 8:00pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/10 "2017-07-14T20:00:47Z")

</div>

I have the same issue with the official 0.6.0 download. BUT, it turns out, it works just fine if you run it from the command line, rather than from visual studio. In the version I built, I get the following error even though the dll is in the bin directory.

> Intel MKL FATAL ERROR: Cannot load mkl\_intel\_thread.dll.

OK, it works, if I copy the mkl\_intel\_thread.dll to the same directory as the .Net executable.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [July 14, 2017, 8:22pm UTC](https://discourse.julialang.org/t/how-correct-open-and-close-julia-dll-in-c/3877/11 "2017-07-14T20:22:04Z")

</div>

That seems unrelated to the stack size issue, just the usual Windows DLL hell 😩.
