1

I am trying to compile a very simple C/C++ program to call Julia functions. Following the instructions that you find on the Julia documentation page, I set up my link path to /Users/william.calhoun/Desktop/romeo/lib/julia looking for libjulia.so and I set up my include path to /Users/william.calhoun/Desktop/romeo/include/julia looking for julia.h

I have a C file called test.c which runs the following code:

#include <stdio.h>
#include "skeleton.h"
#include <julia.h>


int main(int argc, const char * argv[]) {

    jl_init(NULL);

    /* run julia commands */
    jl_eval_string("print(sqrt(2.0))");

    /* strongly recommended: notify julia that the
     program is about to terminate. this allows
     julia time to cleanup pending write requests
     and run all finalizers
     */


    jl_atexit_hook();

   return 0;
}

However this yields the following error:

Undefined symbols for architecture x86_64:
    "_jl_atexit_hook", referenced from:
        _main in test.o
    "_jl_eval_string", referenced from:
        _main in test.o
    "_jl_init", referenced from:
        _main in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am not doing anything other than calling functions defined properly (hopefully) within the Julia source code. What am I doing wrong? This seems like the simplest example and I can't figure it out.

Any help would be much appreciated!

implmentor
  • 1,386
  • 4
  • 21
  • 32
William Calhoun
  • 95
  • 2
  • 12
  • 2
    You need to explicitly link to the Julia library. Are you doing that or just pointing the linker to the directory where it's stored? – Captain Obvlious Aug 17 '15 at 22:41
  • 2
    C is not C++ is not C. Please pick the correct language (C I presume). – too honest for this site Aug 17 '15 at 22:45
  • 4
    Please don't cross-post in multiple forums (https://github.com/JuliaLang/julia/issues/12673). It's the same people answering questions in both places. – StefanKarpinski Aug 18 '15 at 01:50
  • I have explicitly linked to libjulia.dylib and am using C (I merely referenced both because you can do the same in both). Now I get the error that dyld: Library not loaded: @rpath/libjulia-debug.dylib Reason: Image not found – William Calhoun Aug 18 '15 at 14:32

1 Answers1

0

Linking to libjulia (libjulia.dynlib on OS/X)

This error is a result of not linking to libjulia, as all of the symbols (_jl_atexit_hook, _jl_eval_string, _jl_init) are located in that library. Broadly, for all 3 of the following platforms (Windows, OS/X, Linux), the approach is similar, and though the location of the libjulia library is different on Windows than the other 2 this stackoverflow question is applicable. Also to be completely accurate, on OS/X, dynamic libraries have the extension .dynlib not .so as they do on Linux.

The link step

For simplicity, assuming you've compiled to object code (there is a file called embed.o), here's the link step.

cc -o embed embed.o -L/Users/william.calhoun/Desktop/romeo/lib/julia -Wl,-rpath,/Users/william.calhoun/Desktop/romeo/lib/julia -ljulia

There are 2 important things to note here.

  1. Linking using -ljulia will allow the linker to resolve all of the above symbols.
  2. Since this is a dynamic library and that dynamic library is located in a non standard location (e.g. not in /usr/lib), the dynamic linker will not be able to find it at run time unless you give it special instructions on how to find it. The -rpath directive causes the linker to insert the path /Users/william.calhoun/Desktop/romeo/lib/juliainto the list of paths to search.
Community
  • 1
  • 1
waTeim
  • 9,095
  • 2
  • 37
  • 40
  • Thanks for the reply. I have linked against the libjulia.dylib to be exact. Using gcc compiler and XCode, I keep getting the same error. gcc main.c -L/Users/william.calhoun/Desktop/romeo_juliet/lib/julia -Wl,-rpath,/Users/william.calhoun/Desktop/romeo_juliet/lib/julia -ljulia -I/Users/william.calhoun/desktop/romeo_juliet/include/julia Undefined symbols for architecture x86_64: "_jl_atexit_hook", referenced from: _main in main-1ea2de.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – William Calhoun Aug 18 '15 at 14:09
  • OK! So I got it to compile by following your instructions. Except I had to switch the being gcc main.c ..... and include the -I include path However now the output (a.out file) yields the following error upon me ./a.out William's MacBook Air:skeleton william.calhoun$ ./a.out -o System image file "/Users/william.calhoun/Documents/skeleton/skeleton/../lib/julia/sys.ji" not found Where /Users/william.calhoun/Documents/skeleton/skeleton/ is where my main.c and a.out are located. Thanks again for the help! – William Calhoun Aug 18 '15 at 14:48
  • Okay I was able to fix the error. But any help on getting me to compile this in an IDE like Xcode vs compiler in the terminal would be much appreciated! – William Calhoun Aug 18 '15 at 14:57
  • oh, Xcode eh, yea good idea. This is definitely possible, though I have not done so before. I will do this and update the answer. It will take a little while, because the ultimate goal will be to create libjulia in a framework. As a simple experiment, in the meantime you can attempt yourself by simply adding these flags to the Xcode project in compiler and linker flags for the app target while at the same time adding the library. This will result in an app that will only work on your machine though so not very useful, but good practice. – waTeim Aug 18 '15 at 15:41