# Problems with \`dlopen\`

**URL:** https://discourse.julialang.org/t/problems-with-dlopen/98679
**Category:** New to Julia
**Tags:** question, ffi
**Created:** [May 11, 2023, 1:30pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679 "2023-05-11T13:30:57Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 11, 2023, 1:30pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/1 "2023-05-11T13:30:58Z")

</div>

Ok I realize that `dlopen` may not be a new-to-Julia issue, but FFI is important for us to be able to expand usage of Julia in my company. I have read through the manual section on [using C and Fortran](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/) (which is rather good, actually). I think what I am running into here is likely a filesystem path problem.

Here is my demo c code (OS is WSL Ubuntu):

```plaintext
#include <math.h>
// gcc my_exp.c -fPIC -shared -lm -o libmy_exp.so
// gcc -c my_exp.c -o my_exp.o
double my_exp(double x) {
    return exp(x);
}

```

After compiling it as indicated in the comments of the code, I change to the directory that the shared library is in, and start Julia:

```julia
julia> @ccall "./libmyexp".my_exp(3.0::Float64)::Float64
20.085536923187668

julia> using Libdl

julia> h = dlopen("./libmyexp")
Ptr{Nothing} @0x000000000152a330

julia> h = dlopen("./libmyexp.so")
Ptr{Nothing} @0x000000000152a330

```

Am I missing a path or something? Also, is it the “Julian way” to use `dlopen` and `dlsym` when the `@ccall` macro seems to be how you end up calling it anyhow?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 11, 2023, 1:37pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/2 "2023-05-11T13:37:30Z")

</div>

What’s the issue here, exactly? Is it unexpected that `@ccall` already works without using `dlopen` beforehand?

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 11, 2023, 1:41pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/3 "2023-05-11T13:41:23Z")

</div>

Oh, perhaps I should have also put this, niether of the `dlsym` handles seems to work:

```julia
julia> @ccall h.my_exp(3.0::Float64)::Float64
ERROR: TypeError: in ccall, expected Symbol, got a value of type Ptr{Nothing}
Stacktrace:
 [1] top-level scope
   @ ./REPL[5]:1

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 11, 2023, 1:43pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/4 "2023-05-11T13:43:13Z")

</div>

Ah, I see - the macro only directly supports strings and symbols as literals, IIRC. To use the handle, you can use the `ccall` function.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 11, 2023, 1:45pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/5 "2023-05-11T13:45:24Z")

</div>

@Sukera, ok thanks, that can work - just calling it like that.

I guess I had been used to using `dlopen` and `dlsym` in other FFI cases in Python …  
I also tried to make the handle with an absolute path, and that didn’t work either:

```julia
julia> h = dlopen("/home/me/Projects/Julia/libmyexp.so")
Ptr{Nothing} @0x000000000152a330

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 11, 2023, 1:46pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/6 "2023-05-11T13:46:42Z")

</div>

`dlopen` still gives you a handle - that’s just represented by a pointer. Is there another error?

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 11, 2023, 1:47pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/7 "2023-05-11T13:47:35Z")

</div>

```julia
julia> ccall(("my_exp", h), Float64, (Float64,), 3.0)
ERROR: TypeError: in ccall, expected Symbol, got a value of type Ptr{Nothing}
Stacktrace:
 [1] top-level scope
   @ ./REPL[11]:1

```

(deleted something that I found out wasn’t valid)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 11, 2023, 2:14pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/8 "2023-05-11T14:14:45Z")

</div>

Apologies, I got the order mixed up - you need to get the symbol from the dynamic library with `dlsym` first, of course:

```julia
julia> using Libdl

julia> h = dlopen("./libmy_exp.so")
Ptr{Nothing} @0x000055d013c2ce90

julia> hfunc = dlsym(h, :my_exp)
Ptr{Nothing} @0x00007f9b44833109

julia> ccall(hfunc, Float64, (Float64,), 3.0)
20.085536923187668

julia> @ccall $hfunc(3.0::Float64)::Float64
20.085536923187668

```

See also the [docs](https://docs.julialang.org/en/v1/base/c/#Base.@ccall) for `@ccall`.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 11, 2023, 2:24pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/9 "2023-05-11T14:24:32Z")

</div>

Thanks! Apparently I missed the `$` in front of my function handle?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 11, 2023, 2:25pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/10 "2023-05-11T14:25:46Z")

</div>

If you had a function handle, yes - what you’ve shown so far was only the handle for the library 🙂 Note the `hfunc = dlsym(...)`.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [May 11, 2023, 2:27pm UTC](https://discourse.julialang.org/t/problems-with-dlopen/98679/11 "2023-05-11T14:27:05Z")

</div>

Heh, yeah it has the dollar sign right there in the documents … I just went on as if it weren’t there. Thanks.
