Created
October 22, 2016 16:39
-
-
Save GunnarFarneback/df2910fa9460cd267b37416f3efea77b to your computer and use it in GitHub Desktop.
Embedding Julia with dynamic loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_static_loading: test_static_loading.c | |
gcc -Wall test_static_loading.c -ljulia -I ../src -I ../src/support -I../usr/include -L../usr/lib -Wl,-rpath,../usr/lib -o test_static_loading -DJULIA_ENABLE_THREADING=1 -fPIC | |
test_dynamic_loading: test_dynamic_loading.c | |
gcc -Wall test_dynamic_loading.c -ldl -I ../src -I ../src/support -I../usr/include -o test_dynamic_loading -DJULIA_ENABLE_THREADING=1 -fPIC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <dlfcn.h> | |
#include <julia.h> | |
int | |
main(void) | |
{ | |
void *handle; | |
void (*p_jl_init)(const char *); | |
void (*p_jl_atexit_hook)(int); | |
jl_value_t *(*p_jl_eval_string)(const char *); | |
jl_sym_t *(*p_jl_symbol)(const char *); | |
jl_module_t *p_jl_main_module; | |
jl_value_t *(*p_jl_get_global)(jl_module_t *, jl_sym_t *); | |
void *(*p_jl_unbox_voidpointer)(jl_value_t *); | |
handle = dlopen("../usr/lib/libjulia.so", RTLD_LAZY | RTLD_GLOBAL); | |
if (!handle) { | |
fprintf(stderr, "%s\n", dlerror()); | |
exit(EXIT_FAILURE); | |
} | |
dlerror(); /* Clear any existing error */ | |
p_jl_init = dlsym(handle, "jl_init"); | |
p_jl_atexit_hook = dlsym(handle, "jl_atexit_hook"); | |
p_jl_eval_string = dlsym(handle, "jl_eval_string"); | |
p_jl_symbol = dlsym(handle, "jl_symbol"); | |
p_jl_main_module = dlsym(handle, "jl_main_module"); | |
p_jl_get_global = dlsym(handle, "jl_get_global"); | |
p_jl_unbox_voidpointer = dlsym(handle, "jl_unbox_voidpointer"); | |
char *error = dlerror(); | |
if (error != NULL) { | |
fprintf(stderr, "%s\n", error); | |
exit(EXIT_FAILURE); | |
} | |
(*p_jl_init)("../usr/bin"); | |
(*p_jl_eval_string)("include(\"test_loading.jl\")"); | |
jl_sym_t *f_pointer_symbol = (*p_jl_symbol)("f_pointer"); | |
jl_value_t *f_pointer_value = (*p_jl_get_global)(p_jl_main_module, f_pointer_symbol); | |
int (*f_pointer)(int, int) = (*p_jl_unbox_voidpointer)(f_pointer_value); | |
printf("%d\n", (*f_pointer)(3.0, 4.0)); | |
(*p_jl_atexit_hook)(0); | |
dlclose(handle); | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f(x, y) = x + y * y | |
const f_pointer = cfunction(f, Cint, (Cint, Cint)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <julia.h> | |
int | |
main(void) | |
{ | |
jl_init("../usr/bin"); | |
jl_eval_string("include(\"test_loading.jl\")"); | |
jl_sym_t *f_pointer_symbol = jl_symbol("f_pointer"); | |
jl_value_t *f_pointer_value = jl_get_global(jl_main_module, f_pointer_symbol); | |
int (*f_pointer)(int, int) = jl_unbox_voidpointer(f_pointer_value); | |
printf("%d\n", (*f_pointer)(3, 4)); | |
jl_atexit_hook(0); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found the mistake.
p_jl_main_module
should be ajl_module_t **
.