|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <dlfcn.h> |
| 4 | +#include <julia.h> |
| 5 | + |
| 6 | +// Define the type of the function calculate_control |
| 7 | +typedef double (*calculate_control_t)(double r, double y, double uff); |
| 8 | + |
| 9 | +int main() { |
| 10 | + jl_init(); |
| 11 | + |
| 12 | + const char *lib_path = "/home/fredrikb/.julia/dev/DiscretePIDs/examples/juliac/juliac_pid.so"; |
| 13 | + |
| 14 | + // Load the shared library |
| 15 | + printf("Loading juliac_pid.so\n"); |
| 16 | + void *lib_handle = dlopen(lib_path, RTLD_LAZY); |
| 17 | + if (!lib_handle) { |
| 18 | + fprintf(stderr, "Error: Unable to load library %s\n", dlerror()); |
| 19 | + exit(EXIT_FAILURE); |
| 20 | + } |
| 21 | + printf("Loaded juliac_pid.so\n"); |
| 22 | + |
| 23 | + // Locate the calculate_control function |
| 24 | + printf("Finding calculate_control!\n"); |
| 25 | + calculate_control_t calculate_control = (calculate_control_t)dlsym(lib_handle, "calculate_control!"); |
| 26 | + char *error = dlerror(); |
| 27 | + if (error != NULL) { |
| 28 | + fprintf(stderr, "Error: Unable to find symbol calculate_control!: %s\n", error); |
| 29 | + dlclose(lib_handle); |
| 30 | + exit(EXIT_FAILURE); |
| 31 | + } |
| 32 | + printf("Found calculate_control!\n"); |
| 33 | + |
| 34 | + // Call the function |
| 35 | + double r = 0.0, y = 0.0, uff = 0.0; |
| 36 | + double result = calculate_control(r, y, uff); |
| 37 | + printf("calculate_control! returned: %f\n", result); |
| 38 | + |
| 39 | + // Close the library |
| 40 | + dlclose(lib_handle); |
| 41 | + |
| 42 | + jl_atexit_hook(0); |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +// export LD_LIBRARY_PATH=/home/fredrikb/repos/julia/usr/lib:$LD_LIBRARY_PATH |
| 48 | +// gcc -o pid_program test_juliac_pid.c -I /home/fredrikb/repos/julia/usr/include/julia -L/home/fredrikb/repos/julia/usr/lib -ljulia -ldl |
0 commit comments