9 int main(
int argc,
char* argv) {
12 double (*pow)(double, double);
13 const char *dlsym_error;
15 char *library_file =
"/usr/lib/libm.dylib";
16 char *routine_name =
"pow";
17 void (*routine_handle);
20 lib_handle = dlopen(library_file, RTLD_LAZY);
24 printf(
"Cannot open library: %s\n", dlerror());
27 printf(
"Library opened: %s\n", library_file);
33 routine_handle = dlsym(lib_handle, routine_name);
35 dlsym_error = dlerror();
38 printf(
"Cannot load symbol: 'pow': %s\n", dlsym_error);
42 printf(
"Symbol loaded: %s\n", routine_name);
56 args = malloc(n_args*
sizeof(ffi_type*));
57 values = malloc(n_args*
sizeof(
void*));
58 rc = malloc(
sizeof(
double));
60 if(!args || !values || !rc) {
61 printf(
"Could not allocate memory for ffi\n");
64 printf(
"ffi memory allocated\n");
67 for(i=0; i<n_args; i++) {
68 args[i] = &ffi_type_double;
75 if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, n_args, &ffi_type_double, args) == FFI_OK) {
77 ffi_call(&cif, routine_name, rc, values);
79 printf(
"%d\n", (
double)*rc);
83 for(i=0;i<n_args;i++) {