# Wrapping c code and packaging the wrapper

**URL:** https://discourse.julialang.org/t/wrapping-c-code-and-packaging-the-wrapper/32363
**Category:** New to Julia
**Created:** [December 17, 2019, 12:40am UTC](https://discourse.julialang.org/t/wrapping-c-code-and-packaging-the-wrapper/32363 "2019-12-17T00:40:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AlfTetzlaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alftetzlaff/32/13124_2.png) [@AlfTetzlaff](https://discourse.julialang.org/u/AlfTetzlaff)
#### Post date: [December 17, 2019, 12:40am UTC](https://discourse.julialang.org/t/wrapping-c-code-and-packaging-the-wrapper/32363/1 "2019-12-17T00:40:20Z")

</div>

Hey everybody!  
I want to wrap a .so library file which provides access to the cameras of a certain vendor (ZWO ASI). For a start I need it for home use with possible distribution later. However, I don’t manage to create a package which I can import to other projects. I took a look into BinDeps, BinaryBuilder, BinaryProvider and what not and my head is already dizzy - I think this is all way over the top, as my .so file resides on my disk.  
My current folder structure looks like this:

```julia
LibASICamera (this is the package i want to have)
  |--src
  | LibASICamera.jl
  |
  |--gen (contents auto-generated by Clang.jl)
       ctypes.jl
       LibASICamera_api.jl
       LibASICamera_common.jl

ASICamera2 (this is where the c code resides)
  |--include
  | ASICamera2.h
  |
  |--lib/x64
       libASICamera2.so

```

Here an example function from LibASICamera\_api.jl:

```julia
function ASIGetNumOfConnectedCameras()
    ccall((:ASIGetNumOfConnectedCameras, libASICamera2), Cint, ())
end

```

One can see that the variable libASICamera2 is undefined at this point. So I thought it is best to point Libdl to the library folder and define the variable in LibASICamera.jl:

```julia
module LibASICamera

import Libdl
using CEnum

push!(Libdl.DL_LOAD_PATH, "/home/.../ASICamera2/lib/x64")
libASICamera2 = "libASICamera2"

include(joinpath(@ __DIR__ , "..", "gen", "ctypes.jl"))
include(joinpath(@ __DIR__ , "..", "gen", "LibASICamera_common.jl"))
include(joinpath(@ __DIR__ , "..", "gen", "LibASICamera_api.jl"))

export ...

end # module

```

This does not work. When I load the package by “using LibASICamera” and call the example function from above I get an error:

```julia
TypeError: in ccall: first argument not a pointer or valid constant expression, expected Ptr, got Tuple{Symbol,String} ASIGetNumOfConnectedCameras() at LibASICamera_api.jl:6

```

On the contrary, when I use the REPL and interactively push the load path and call the function, everything works as expected. Therefore I assume that the library was not loaded correctly and/or that I have a misconception about how loading libraries works.

What do I have to change to make it work? 🙂

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 17, 2019, 6:43am UTC](https://discourse.julialang.org/t/wrapping-c-code-and-packaging-the-wrapper/32363/2 "2019-12-17T06:43:05Z")

</div>

I would start with

```julia
const libASICamera2 = "libASICamera2"

```

Then I would skip the `DL_LOAD_PATH` and use a full path directly for `libASICamera2`.

---

<div class="post-metadata">

### Author: ![AlfTetzlaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alftetzlaff/32/13124_2.png) [@AlfTetzlaff](https://discourse.julialang.org/u/AlfTetzlaff)
#### Post date: [December 17, 2019, 2:12pm UTC](https://discourse.julialang.org/t/wrapping-c-code-and-packaging-the-wrapper/32363/3 "2019-12-17T14:12:09Z")

</div>

Thank you! For some reason I don’t understand declaring the variable const did the trick.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 19, 2019, 1:43pm UTC](https://discourse.julialang.org/t/wrapping-c-code-and-packaging-the-wrapper/32363/4 "2019-12-19T13:43:52Z")

</div>

`ccall` is a part of the low-level infrastructure of Julia and despite the looks not an ordinary function. The need for `const` here is a consequence of that.
