# Passing a String with file name of shared library to ccall

**URL:** <https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799>\
**Category:** New to Julia\
**Created:** [June 20, 2020, 11:19pm UTC](https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799 "2020-06-20T23:19:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [June 20, 2020, 11:19pm UTC](https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799/1 "2020-06-20T23:19:35Z")

</div>

I have a shared C library with its full name in `ENV["LIBSIMC"]`:

```julia
julia> ENV["LIBSIMC"]
"/home/paul/st/simc/bin/libsimc.so"

```

My call works fine with a literal constant, e.g.:

```julia
ccall((:logText, "/home/paul/st/simc/bin/libsimc.so"), Cvoid, (Cstring,), "something, something")

```

but it fails with library name in a String:

```julia
julia> ccall((:logText, ENV["LIBSIMC"]), Cvoid, (Cstring,), "something, something")
ERROR: TypeError: in ccall: first argument not a pointer or valid constant expression, expected Ptr, got Tuple{Symbol,String}

```

How to fix it?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 21, 2020, 12:27am UTC](https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799/2 "2020-06-21T00:27:02Z")

</div>

You can’t do that. The name and library to ccall has to be a constant. If there’s no way to determine the name at pre-compilation time, you should use `dlopen` and `dlsym` manually. I believe `PyCall` uses it.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [June 21, 2020, 1:09am UTC](https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799/3 "2020-06-21T01:09:38Z")

</div>

Thank you. I can work around it. I tried a relative path and I was surprised by the failure:

```julia
julia> ccall((:shutdown, "~/st/simc/bin/libsimc.so"), Cvoid, ())
ERROR: could not load library "~/st/simc/bin/libsimc.so"
~/st/simc/bin/libsimc.so: cannot open shared object file: No such file or directory
Stacktrace:
 [1] top-level scope at ./REPL[2]:1

shell> file ~/st/simc/bin/libsimc.so
/home/paul/st/simc/bin/libsimc.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=e9c5bf2bd0401b726d2a37c3162c93951f6f5736, not stripped

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 21, 2020, 1:24am UTC](https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799/4 "2020-06-21T01:24:39Z")

</div>

You did not try a relative path. You tried a shell expansion. `~` is the absolute path to your home and it’s a shell function. Relative path does work but it won’t be relative to somewhere you want.

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [June 22, 2020, 6:22pm UTC](https://discourse.julialang.org/t/passing-a-string-with-file-name-of-shared-library-to-ccall/41799/5 "2020-06-22T18:22:13Z")

</div>

For the sake of completeness and as an answer to future searches, here is the way to use a `String` for library path:

```julia
libPath = pwd()
push!(Base.DL_LOAD_PATH, libPath)
ccall((:logText, "libsimc.so"), Cvoid, (Cstring,), "something, something")

```
