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

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 :frowning: 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” :

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” :

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

Here’s my C file “intcjulia.c”

#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

1 Like

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

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

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 by @sdanisch, he uses a line like this to compile a specific function method:

precompile(foo, (Float64, Float64))

Maybe this is what you need, to?

Have a look at static-julia.

2 Likes

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.

1 Like

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

Please vote for my feature request:

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

1 Like

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"

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.