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

Dear @kristoffer.carlsson

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

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

I have created a folder Projectfolder using generate Projectfolder and then activate Projectfolder, which has:
33
I opened the generated file./Projectfolder/scr/Projectfolder.jl and put in it the above function, i.e.

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

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

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”

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

1
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?

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

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.

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

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

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

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


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

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

You can take a look at the docs here: Libraries · PackageCompiler as well as a more full example in GitHub - simonbyrne/libcg.

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.

1 Like

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.

1 Like

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

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).

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:

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?