# Call c++ functions using ccall

**URL:** <https://discourse.julialang.org/t/call-c-functions-using-ccall/94799>\
**Category:** General Usage\
**Created:** [February 17, 2023, 5:12pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799 "2023-02-17T17:12:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [February 17, 2023, 5:12pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799/1 "2023-02-17T17:12:13Z")

</div>

I have a library in c++, from which I want to call some function. I have tried `CxxWrap`, which kind of work and seems not stable and I sometimes got segmentation faults.

I tried to call the function using `ccall((:myFunc, "....../libmyLib"), .....)`, but got errors like

```julia
ERROR: could not load symbol "myFunc":
....../libmyLib.so: undefined symbol: myFunc

```

I’ve checked the library with `nm -gDC`, and the symbol is indeed there.  
Does anyone have ideas about this issue? Thanks.

---

<div class="post-metadata">

**Author:** ![Taaitaaiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taaitaaiger/32/49792_2.png) [@Taaitaaiger](https://discourse.julialang.org/u/Taaitaaiger)\
**Post date:** [February 17, 2023, 5:28pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799/2 "2023-02-17T17:28:18Z")

</div>

Did you define the function in an extern “C” block?

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [February 17, 2023, 5:32pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799/3 "2023-02-17T17:32:06Z")

</div>

No…and I just realized that the c symbols are quite different from c++'s symbols. If I use `:_Z2myFuncPdm` it works.

How can I use extern “C” to get a sensible symbol?

---

<div class="post-metadata">

**Author:** ![Taaitaaiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taaitaaiger/32/49792_2.png) [@Taaitaaiger](https://discourse.julialang.org/u/Taaitaaiger)\
**Post date:** [February 17, 2023, 5:38pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799/4 "2023-02-17T17:38:28Z")

</div>

IIRC this should be sufficient:

```plaintext
extern "C" {
    void myFunc() {
        // impl...
    }
}

```

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [February 17, 2023, 6:08pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799/5 "2023-02-17T18:08:34Z")

</div>

That works. Thank you!

---

<div class="post-metadata">

**Author:** ![Taaitaaiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taaitaaiger/32/49792_2.png) [@Taaitaaiger](https://discourse.julialang.org/u/Taaitaaiger)\
**Post date:** [February 17, 2023, 6:39pm UTC](https://discourse.julialang.org/t/call-c-functions-using-ccall/94799/6 "2023-02-17T18:39:28Z")

</div>

No worries, I’m happy to hear it solved your issue!
