# Recommended workflow to prepare a package to use it as a dll with PackageCompiler.jl

**URL:** <https://discourse.julialang.org/t/recommended-workflow-to-prepare-a-package-to-use-it-as-a-dll-with-packagecompiler-jl/59260>\
**Category:** General Usage\
**Tags:** question, embedding, package-compiler, c\
**Created:** [April 14, 2021, 9:42am UTC](https://discourse.julialang.org/t/recommended-workflow-to-prepare-a-package-to-use-it-as-a-dll-with-packagecompiler-jl/59260 "2021-04-14T09:42:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [April 14, 2021, 9:42am UTC](https://discourse.julialang.org/t/recommended-workflow-to-prepare-a-package-to-use-it-as-a-dll-with-packagecompiler-jl/59260/1 "2021-04-14T09:42:40Z")

</div>

I am currently trying to compile one of my packages for use as dll with `PackageCompiler.jl`.  
This somewhat a follow up post of [Package Compiler error when reproducing documentation example](https://discourse.julialang.org/t/package-compiler-error-when-reproducing-documentation-example/58926), yet more generic.

During the aforementioned discussion it seems that to use a package as a dll one has to perform multiple modifications with respect to the initial pure julia code. Among these modifications, I can think of :

- change julia types to C understandable types, for both input and return values
- decorate function declarations using the `Base.@ccallable` macro

The general question about these modifications (and the probable others I did not think about) is : how to make them as transparent or _painless_ as possible for the user ?

In fact, most examples I read to export a C-usable function mention scalar types, e.g.

```julia
Base.@ccallable function my_main_function(x::Cfloat,y::Cfloat)::Cfloat
        z = 0
        for i in 1:x
            z += i ^ y
        end
        return z
    end

```

I can only assume that the initial function was something like

```julia
function my_main_function(x::Float32,y::Float32)::Float32
        z = 0
        for i in 1:x
            z += i ^ y
        end
        return z
    end

```

From this basic example, some questions arise :

- How to avoid coding twice every function ?  
If the input and return types are explicitly declared, this function can only be used with Ctypes. But when developping the julia package it seems less than ideal to be unable to use julia types when trying such functions.
- How to handle array type, or structs conversion ?  
When packages become more complex, and structures are used to pass multiple data to different functions, how to avoid all the manual type conversion ? It may be a job for `Base.cconvert` or `Base.unsafe_convert`, but that’s a highly dubious guess

These points may have been addressed before, but after having spent multiple days on this, it is still very unclear to me what the correct workflow should be.
