# problem with ccall in v1.0

**URL:** <https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869>\
**Category:** New to Julia\
**Created:** [October 4, 2018, 11:55am UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869 "2018-10-04T11:55:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 4, 2018, 11:55am UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/1 "2018-10-04T11:55:38Z")

</div>

Hi there  
in version 0.6.4 this runs perfectly:  
cfi = “/tcenas/home/anderson/lib/eocfi/lib/LINUX64/xxx/xx/libcfi.so”  
v = ccall((:xl\_verbose,cfi),Void,())

but in version 1.0 the lines:  
cfi = “/tcenas/home/anderson/lib/eocfi/lib/LINUX64/xxx/xx/libcfi.so”  
v = ccall((:xl\_verbose,cfi),CVoid,())

produce the error:  
signal (11): Segmentation fault  
in expression starting at /tcenas/home/anderson/julia/bb.jl:8  
\_ZN4llvm3Use3setEPNS\_5ValueE at /usr/local/bin/julia-1.0.0/bin/…/lib/julia/libLLVM-6.0.so (unknown line)  
\_ZN4llvm8LoadInstC2EPNS\_4TypeEPNS\_5ValueEPKcbPNS\_11InstructionE at /usr/local/bin/julia-1.0.0/bin/…/lib/julia/libLLVM-6.0.so (unknown line)  
CreateLoad at /buildworker/worker/package\_linux64/build/usr/include/llvm/IR/IRBuilder.h:1175  
emit\_varinfo at /buildworker/worker/package\_linux64/build/src/codegen.cpp:3374  
emit\_expr at /buildworker/worker/package\_linux64/build/src/codegen.cpp:3455  
…

Any help, guidance or a solution to this problem would very be very much appreciated.  
Craig

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 4, 2018, 11:57am UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/2 "2018-10-04T11:57:39Z")

</div>

The replacement for `Void` is `Cvoid`, not `CVoid`. Is that a typo? Can you give more details on the called function?

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 4, 2018, 12:53pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/3 "2018-10-04T12:53:24Z")

</div>

yep, that was a typo and it should have been “Cvoid” in the example code (it was “Cvoid” in the program that produces the error,) Sorry.  
The called function is part of a large library and just sets an internal flag so that other functions know to print errors and warnings to stdout,

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 4, 2018, 1:28pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/4 "2018-10-04T13:28:25Z")

</div>

Without a way to reproduce the problem I’m afraid it will be hard to help. If you can’t make the library available to Julia developers, you could try to get a better backtrace using the julia-debug excutable, and/or by running it under Valgrind.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 4, 2018, 1:30pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/5 "2018-10-04T13:30:42Z")

</div>

> [@pixelate](#):
>
> \_ZN4llvm8LoadInstC2EPNS\_4TypeEPNS\_5ValueEPKcbPNS\_11InstructionE at **/usr/local/bin/julia-1.0.0/bin/…/lib/julia/libLLVM-6.0.so** (unknown line)  
> CreateLoad at **/buildworker/worker/package\_linux64/build/usr/include/llvm** /IR/IRBuilder.h:1175

~~I would be suspicios about mixing llvm versions…~~

~~What is `ldd /tcenas/home/anderson/lib/eocfi/lib/LINUX64/xxx/xx/libcfi.so` output?~~

EDIT: I think that @jpsamaroo is right.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [October 4, 2018, 1:58pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/6 "2018-10-04T13:58:01Z")

</div>

I’m pretty sure that’s just a path that’s baked in while Julia binaries are being compiled by the official buildbots.

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 4, 2018, 7:12pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/7 "2018-10-04T19:12:08Z")

</div>

OK, I will create a simple library that I can make available and see if the problem reoccurs (but I suspect that the problem is independent of the library because if I deliberately give the wrong path name to the library then I get the same error message). I will do this next week and in the meantime thanks for your interest and suggestions.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 4, 2018, 8:51pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/8 "2018-10-04T20:51:27Z")

</div>

I tried but have no problem. Here is my MWE:

```julia
/tmp/solib$ cat a.cpp
#include <fstream>

extern "C" {
void fnc(){
    std::ofstream f("/tmp/out");
    f << "hello" << std::endl;
    f.close();
}
}

```

Building library:

```julia
/tmp/solib$ g++ -shared -fPIC a.cpp -o a.so

```

I had to restart Julia after changes in library!

```julia
julia> ccall((:fnc, "/tmp/solib/a.so"), Cvoid, ())

shell> cat /tmp/out
hello

```

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [October 4, 2018, 9:03pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/9 "2018-10-04T21:03:28Z")

</div>

If you get this even with an invalid library name, then it’s probably a Julia bug. I noticed that you’re running Julia 1.0.0, which has had numerous segfault issues addressed in more recent versions. Any chance you could upgrade to 1.0.1 and retry?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 4, 2018, 9:05pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/10 "2018-10-04T21:05:34Z")

</div>

This might be the line that segfaults, but is it the only ccall in your code? Did you perhaps pass some Julia data structure to your library in an earlier call? It could be the case that you’ve passed some Julia object to C, that object was no longer referenced on the Julia side and was therefore garbage collected, and then your C code tried to use that object, resulting in a use-after-free error. The GC in 0.7/1.0 seems to be a bit better at freeing objects as soon as they’re no longer referenced by Julia, which has had a side effect of making bugs like the one I described more obvious.

Of course without more info about what your code is doing, I’m really just guessing.

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 8, 2018, 6:20pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/11 "2018-10-08T18:20:45Z")

</div>

Hi jpsamaroo and nalimilan

C code:  
#include \<stdio.h\>  
int test( ) { printf(“testy\n”); return 999; }

compiled with:  
gcc -shared -fPIC test.c -o libtest.so

julia code:  
lib = “/tcenas/home/anderson/julia/xxx/libtest.so”  
v = ccall((:test,lib),Int32,())  
println(v)

Runs perfectly with 0.6.4 and segfaults with 1.0.0  
So now I will install 1.0.1 and retry …

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 9, 2018, 7:34am UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/12 "2018-10-09T07:34:30Z")

</div>

Works here on 1.0.1 (after replacing the `lib` argument with the literal path).

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 10, 2018, 11:14am UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/13 "2018-10-10T11:14:35Z")

</div>

Well I installed 1.0.1 and retried and the example still segfaults with the same error message as before. This is disappointing.

But interestingly the example code provided by Liso (thanks Liso!) seems to run ok (but did not print anything to stdout).

As soon as I get time I will play around a bit and see if I can work out what is causing the situation.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 10, 2018, 12:03pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/14 "2018-10-10T12:03:04Z")

</div>

Can you post the exact Julia code you used? As I noted I had to do this instead, since `ccall` needs a literal string:

```julia
v = ccall((:test, "/tcenas/home/anderson/julia/xxx/libtest.so"), Cint, ())

```

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 10, 2018, 5:48pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/15 "2018-10-10T17:48:35Z")

</div>

Hi nalimilan

The file test.c contains:

#include \<stdio.h\>  
int test( )  
{  
printf(“testy\n”);  
return 999;  
}

and is compiled with

gcc -shared -fPIC test.c -o libtest.so

and the julia file m.jl contains:

function test()  
lib = “/tcenas/home/anderson/julia/xxx/libtest.so”  
v = ccall((:test,lib),Int32,())  
println(v)  
end

println(“hello”)  
test()  
println(“ok”)

where /tcenas/home/anderson/julia/xxx/libtest.so is the full pathname to the output file produced by gcc.

The julia program works fine with 0.6.4 but segfaults with 1.0.0 and 1.0.1

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 10, 2018, 6:04pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/16 "2018-10-10T18:04:45Z")

</div>

… and bizarrely when I change the key line in the julia code to

v = ccall((:test,“/tcenas/home/anderson/julia/xxx/libtest.so”),Int32,())

or just

v = ccall((:test,“./libtest.so”),Int32,())

then everything works just fine.

This makes no sense to me but a working program is better than one that segfaults!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 10, 2018, 6:10pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/17 "2018-10-10T18:10:33Z")

</div>

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 10, 2018, 6:13pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/18 "2018-10-10T18:13:03Z")

</div>

> [@pixelate](#):
>
> v = ccall((:test,“./libtest.so”),Int32,())

Confirmed with Ubuntu in a Windows Linux Subsystem  
Linux Firebolt 4.4.0-17134-Microsoft #285-Microsoft Thu Aug 30 17:31:00 PST 2018 x86\_64 x86\_64 x86\_64 GNU/Linux

Bizarre!

---

<div class="post-metadata">

**Author:** ![pixelate](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@pixelate](https://discourse.julialang.org/u/pixelate)\
**Post date:** [October 10, 2018, 7:06pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/19 "2018-10-10T19:06:32Z")

</div>

ok, looking more closely at the documentation for v0.6 and v1.0 I find:

Shared libraries and functions are referenced by a tuple of the form `(:function, "library")`

so it seems that (a) the library name must be a literal string, and (b) this wasn’t enforced in 0.6 but is necessary in 1.0

It looks like the whole saga has been caused by me not reading the docs with enough attention to detail. My apologies for this, and many thanks to everyone who has taken the time to post suggestions and tips here.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 10, 2018, 7:48pm UTC](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869/20 "2018-10-10T19:48:49Z")

</div>

But I wouldn’t expect a segfault. Do you really mean that it crashes? Can you get a backtrace?

[Next page](https://discourse.julialang.org/t/problem-with-ccall-in-v1-0/15869.md?page=2)
