Skip to content

Commit 4f3e47d

Browse files
committed
add c test program
1 parent e0cd780 commit 4f3e47d

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

examples/juliac/juliac_pid.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ end
4646

4747
# compile using something like
4848
# cd(@__DIR__)
49-
# run(`julia +nightly --project --experimental /home/fredrikb/repos/julia/contrib/juliac.jl --output-lib juliac_pid --trim=unsafe-warn --compile-ccallable juliac_pid.jl`)
49+
# run(`/home/fredrikb/repos/julia/julia --project --experimental /home/fredrikb/repos/julia/contrib/juliac.jl --output-lib juliac_pid --experimental --trim=unsafe-warn --compile-ccallable juliac_pid.jl`)
5050
# run(`ls -ltrh`) # marvel at the smallness of the binary

examples/juliac/test_juliac_pid.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)