# How to create dll file from my julia code via PackageCompiler?

**URL:** <https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 18, 2022, 3:19pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702 "2022-02-18T15:19:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 3:19pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/1 "2022-02-18T15:19:38Z")

</div>

Dear @kristoffer.carlsson

I am trying to practice creating a `dll` file of a simple code called `sourceCode.jl` defined as below:

```julia
Base.@ccallable function test()::Cint
    return(2)
end

```

I have created a folder `Projectfolder` using `generate Projectfolder` and then `activate Projectfolder`, which has:  
 ![33](https://global.discourse-cdn.com/julialang/original/3X/3/1/31e5c21aa27a958e0bbeaa6783f8a321f9d95357.png)  
I opened the generated file`./Projectfolder/scr/Projectfolder.jl` and put in it the above function, i.e.

```julia
module Projectfolder
Base.@ccallable function test()::Cint
    return(2)
end
end # module

```

Then, I created a folder “./Projectfolder/Generated” and executed the below:

```julia
create_library("$(@ __DIR__ )/Projectfolder", "$(@ __DIR__ )/Projectfolder/Generated"; force=true, lib_name="libtest", version=v"1.0.0")

```

It finishes (with the below warning:) by creating two folders “bin” and “include”

```julia
┌ Warning: it is not recommended to create an app/library without a preexisting manifest
└ @ PackageCompiler 

```

![1](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d7840f2314013780b063d81f0f27b8cdb7f25ce.png)  
1- Are all the above steps are correct?  
2- How can I solve the above warning?  
3- I expect to have a file `libtest.dll` in this path `./Projectfolder/Generated/bin`, but I did not find. So, does this mean that the compilation is not done correctly?

---

<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:** [February 18, 2022, 9:36pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/2 "2022-02-18T21:36:59Z")

</div>

> [@Amro](#):
>
> 2- How can I solve the above warning?

Run `pkg> instantiate` in your project before you run `create_library`.

> [@Amro](#):
>
> I expect to have a file `libtest.dll` in this path `./Projectfolder/Generated/bin` , but I did not find. So, does this mean that the compilation is not done correctly?

Make sure you are using the latest PackageCompiler version (2.0.5). There was a bug in earlier version that made the library be called `sys.dll` on windows.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 10:43pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/3 "2022-02-18T22:43:01Z")

</div>

@kristoffer.carlsson Thank you very much!  
1- I have inserted `instantiate` be fore running `create_library` (as shown below),

```julia
using PackageCompiler: PackageCompiler, create_sysimage, create_app, create_library
using Pkg
ENV["JULIA_DEBUG"] = "PackageCompiler"
mkpath(joinpath("$(@ __DIR__ )/CreatLib", "Generated"))
Pkg.activate("$(@ __DIR__ )/CreatLib")
Pkg.resolve()
Pkg.instantiate(; verbose = false)
create_library("$(@ __DIR__ )/CreatLib", "$(@ __DIR__ )/CreatLib/Generated"; force=true, lib_name="HiAmro", version=v"1.0.0")

```

however, I am still having this the warning of `Warning: it is not recommended to create an app/library without a preexisting manifest`

 ![22](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd2e3ac1f85bb08ec3ca90950bacac1ec399f9c0.png)

Do I made something wrong?

2- I have update PackageCompiler to `version (2.0.5)`, and now I can see a my library name `HiAmro.dll` along with other 53 dll files. If I need to use my dll somewhere, do I need to put all other files with mine `HiAmro.dll` in the same folder to let it work? I tried to test the `HiAmro.dll` by running the below but it gave me a fast message which disappeared and then and

```julia
ccall((:test,"$(@ __DIR__ )/bin/HiAmro.dll"),Int32,(Int,),1)

```

 ![1](https://global.discourse-cdn.com/julialang/original/3X/c/0/c0ecbbaa57ba495dcc8fa48d67254604a2a6c941.png)  
 ![2](https://global.discourse-cdn.com/julialang/original/3X/a/3/a364332319d4cb2a52b72eae36870302aba129ba.png)

3- What is the function of `../include/julia_init.jl`? Do I need to put it somewhere also or call it?

---

<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:** [February 18, 2022, 11:03pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/4 "2022-02-18T23:03:44Z")

</div>

> [@Amro](#):
>
> Do I made something wrong?

Hm, not sure, I can take a look in a while to see what is going on.

> [@Amro](#):
>
> If I need to use my dll somewhere, do I need to put all other files with mine `HiAmro.dll` in the same folder to let it work?

You can take a look at the docs here: [Libraries · PackageCompiler](https://julialang.github.io/PackageCompiler.jl/dev/libs.html) as well as a more full example in [GitHub - simonbyrne/libcg](https://github.com/simonbyrne/libcg).

> [@Amro](#):
>
> 3- What is the function of `../include/julia_init.jl` ? Do I need to put it somewhere also or call it?

I am not sure what you are reffering to. Do you mean `include/julia_init.h`? That contains the declaration of the “entry points” to the library and you would typically include that file in a c program that you want to call the library with.

---

<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:** [February 18, 2022, 11:04pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/5 "2022-02-18T23:04:35Z")

</div>

> [@Amro](#):
>
> I tried to test the `HiAmro.dll` by running the below but it gave me a fast message which disappeared and then and

You cannot ccall into the generated library. It is a technical limitation that you cannot call a julia library from another julia session. You can call it from C or Python for example.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 18, 2022, 11:13pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/7 "2022-02-18T23:13:45Z")

</div>

Thank you very much. I will try to use it from a different tool

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [February 21, 2022, 7:32pm UTC](https://discourse.julialang.org/t/how-to-create-dll-file-from-my-julia-code-via-packagecompiler/76702/9 "2022-02-21T19:32:38Z")

</div>

> [@kristoffer.carlsson](#):
>
> You cannot ccall into the generated library. It is a technical limitation that you cannot call a julia library from another julia session. You can call it from C or Python for example.

Dear @kristoffer.carlsson could you give me a hand here please!  
I am trying to load my generated library (“HiAmro.dll”) for the above function (cited below again for reference).

```julia
Base.@ccallable function test()::Cint
    return(2)
end

```

I am trying to call this function via OpenModelica tool. So, I created a model in Modelica in a package called `OpenEMTP` and I put inside it the generated files as follow; …\OpenEMTP\Resources\source\_julia\include\julia\_init.h and …\OpenEMTP\Resources\source\_julia\bin\HiAmro.dll. I have build the below package/model in OMEdit:

```modelica
within OpenEMTP;
package TestJuliaPackage
  extends UserGuide.Icons.Library;

  function Externaljuliafunc1
    extends Modelica.Icons.Function;
    output Real y;
    external y=test() annotation(Include="#include \"julia_init.h\"", IncludeDirectory="modelica://OpenEMTP/Resources/source_julia/include",Library = "HiAmro.dll",LibraryDirectory="modelica://OpenEMTP/Resources/source_julia/bin");
  end Externaljuliafunc1;

  model ExternaljuliaLibraries
  output Real y;
  equation
  y=Externaljuliafunc1();
  end ExternaljuliaLibraries;

end TestJuliaPackage;

```

1- Is it enough to call the function `test()` or I need also to call `julia_init()` and `shutdown_julia()` somewhere? If so, could you please guide me?  
2- What does `Makefile` do? Do I need to include it also somewhere in the destination tool?
