# How to use a julia compiled object from within a C program

**URL:** <https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475>\
**Category:** New to Julia\
**Tags:** embedding, compilation\
**Created:** [January 19, 2018, 2:38pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475 "2018-01-19T14:38:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nextr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nextr/32/3131_2.png) [@nextr](https://discourse.julialang.org/u/nextr)\
**Post date:** [January 19, 2018, 2:38pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/1 "2018-01-19T14:38:38Z")

</div>

Hi all !

I need to develop an interface to use an existing Julia project from within a C porgram.  
As I am VERY newbie in Julia, I started creating a VERY simple Julia script to better understand the operation mode.

The steps I need to achieve are (I guess) :

- Write a simple julia script
- Create an object (using output-o compilation option)
- Create a shared object (by gcc -share)
- Create a .h header to use this library from C
- Use the Julia library from C  
But I am experiencing problems with the last step ☹ I just don’t know (and don’t find in Julia doc) how to write the .h file compatible with julia-native library. Thus, when I try to compile my C program I obtain “undefined reference” to my julia function.

Here’s my julia script “MULT.ji” :

```julia
module MULT

   export doubler

   function doubler(x)
      x*2
   end

   function tripler(x)
      x*3
   end

   function quadrupler(x)
      x*4
   end
end

```

Here’s my header “mult.h” :

```julia
int doubler(int x);
int tripler(int x);
int quadrupler(int x);

```

Here’s my C file “intcjulia.c”

```julia
#include "mult.h"
#include <stdio.h>

int main(){
  printf("double de 2 : %u ", doubler(2));
}

```

And here’s how I compiled :

#creation du .JI et du .O

> ./bin/julia --output-ji testji.ji --output-o testo.o MULT.jl

#creation du .SO

> gcc -shared -o libmult.so testo.o

#compilation du C

> gcc -L. -Llib/ intcjulia.c -lmult -ljulia

What am I doing wrong ? Where can I find some doc about this issue ? Does anybody can help me ?  
Thank you in advance.

Nextor

---

<div class="post-metadata">

**Author:** ![Philip\_Stuckey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip_stuckey/32/4000_2.png) [@Philip\_Stuckey](https://discourse.julialang.org/u/Philip_Stuckey)\
**Post date:** [January 19, 2018, 3:16pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/2 "2018-01-19T15:16:43Z")

</div>

If I had to guess I’d say that the name mangling in Julia is different from C, so the linker is looking for the wrong mangled name

---

<div class="post-metadata">

**Author:** ![nextr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nextr/32/3131_2.png) [@nextr](https://discourse.julialang.org/u/nextr)\
**Post date:** [January 19, 2018, 4:12pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/3 "2018-01-19T16:12:10Z")

</div>

Thank you for your response Philip, but I don’t really understand. You mean that the name mangling (such as Upper or lower case or underscores) is incorrect ?  
Which is the correct way ?  
In you opinion, my .H file, and mainly my .C file are correct ?  
Is it the right way to make julia code visible ?  
Thanks

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [January 19, 2018, 4:29pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/4 "2018-01-19T16:29:08Z")

</div>

In your Julia code, you don’t specify any types, but in your C header, you assume everything is `int`?

I’m not an expert myself, but in a recent [article](https://medium.com/@sdanisch/compiling-julia-binaries-ddd6d4e0caf4) by @sdanisch, he uses a line like this to compile a specific function method:

```julia
precompile(foo, (Float64, Float64))

```

Maybe this is what you need, to?

---

<div class="post-metadata">

**Author:** ![lucatrv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucatrv/32/5650_2.png) [@lucatrv](https://discourse.julialang.org/u/lucatrv)\
**Post date:** [January 19, 2018, 10:57pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/5 "2018-01-19T22:57:17Z")

</div>

Have a look at [static-julia](https://github.com/JuliaComputing/static-julia).

---

<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:** [January 20, 2018, 12:02am UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/6 "2018-01-20T00:02:44Z")

</div>

If it is feasible to compile/refactor your C program as a library, it is _much_ easier to call C from Julia (or any other high level language) than the other way around.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 20, 2018, 1:36pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/7 "2018-01-20T13:36:43Z")

</div>

Please vote for my feature request:  
[https://github.com/JuliaComputing/static-julia/issues/42](https://github.com/JuliaComputing/static-julia/issues/42)

---

<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:** [January 20, 2018, 2:05pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/8 "2018-01-20T14:05:53Z")

</div>

> Please vote for my feature request:

This is typically counter-productive. Recruit someone to work on your feature instead.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 20, 2018, 2:17pm UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/9 "2018-01-20T14:17:26Z")

</div>

Why should this be counter-productive?  
Other open source projects use “stars” of github issues and feature requests to prioritize what to implement in the next release, for example the Python IDE Spyder.  
See:  
[https://github.com/spyder-ide/spyder/issues?q=is%3Aopen+is%3Aissue+label%3A"11–20+stars"](https://github.com/spyder-ide/spyder/issues?q=is%3Aopen+is%3Aissue+label%3A%2211%E2%80%9320+stars%22)

---

<div class="post-metadata">

**Author:** ![nextr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nextr/32/3131_2.png) [@nextr](https://discourse.julialang.org/u/nextr)\
**Post date:** [January 22, 2018, 9:15am UTC](https://discourse.julialang.org/t/how-to-use-a-julia-compiled-object-from-within-a-c-program/8475/10 "2018-01-22T09:15:29Z")

</div>

Thank you for your response stev, but i my case, the existing pattern it is not possible to invert dependencies. It is the existing C program that needs to call functions of the existing Julia module.
