# Compile (and call) a C function from Julia

**URL:** <https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770>\
**Category:** Offtopic\
**Created:** [May 7, 2018, 10:55pm UTC](https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770 "2018-05-07T22:55:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [May 7, 2018, 10:55pm UTC](https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770/1 "2018-05-07T22:55:10Z")

</div>

I wonder if it would make sense that Julia enables one to call a C function described by its source code…  
ccall("  
#include \<stdio.h\>  
int main()  
{  
// printf() displays the string inside quotation  
printf(“Hello, World!”);  
return 0;  
}")

---

<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:** [May 7, 2018, 11:22pm UTC](https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770/2 "2018-05-07T23:22:30Z")

</div>

Cxx.jl has that function.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [May 7, 2018, 11:29pm UTC](https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770/3 "2018-05-07T23:29:06Z")

</div>

Thank you !!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 21, 2018, 3:58pm UTC](https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770/4 "2018-05-21T15:58:07Z")

</div>

Note that if you have a C compiler installed, you can invoke it an load the resulting code pretty easily from Julia. See e.g. the first example in:

[https://github.com/stevengj/18S096/blob/master/lectures/lecture1/Boxes-and-registers.ipynb](https://github.com/stevengj/18S096/blob/master/lectures/lecture1/Boxes-and-registers.ipynb)

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [May 21, 2018, 5:35pm UTC](https://discourse.julialang.org/t/compile-and-call-a-c-function-from-julia/10770/5 "2018-05-21T17:35:03Z")

</div>

Thank you very much for the link ! The conciseness of the C function build looks like blackmagic 😮 I should add this one to [Sum performance for Array{Float64,2} elements - #14 by LaurentPlagne](https://discourse.julialang.org/t/sum-performance-for-array-float64-2-elements/10873/14) benchmark curves (I think I remember that gcc did not perform very well on that specific loop).
