Implement a Program that consumes a .so file using runtime linking that calls an embedded header file

I need to implement a program that uses runtime linking and calls a created header file. The header file is simple, it defines a function double mean(double, double); This function (calc_mean.h) accepts two doubles and returns the mean value between the two. I have the .so file also called libcalc_mean.so

I do not have an IDE, so I am trying to run this using GCC.

My first question is this: How do I run this using GCC?

Second: Can someone tell me if I am doing the right thing here in my code? I feel like it isnt correct, but I can't tell because I can't compile just yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <calc_mean.h> //using calc_mean.h header

int main(int argc, char **argv)
{
    void *calculate;
    double (*mean);
    char *error;

   //open .so file using lazy binding
   //print error if program is not calculated or handled correctly
   calculate = dlopen("./Desktop/NASCENT/InterviewProj2/libcalc_mean.so", RTLD_LAZY);
    if (!calculate) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }

   dlerror(); //Clear any existing error

   *(void **) (&mean) = dlsym(calculate, "avg");

   //check for dl errors
   if ((error = dlerror()) != NULL)  {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

   //call mean function and print answer
   printf("%f\n", (*mean));
    dlclose(calculate);
    exit(EXIT_SUCCESS);
}
I'm not to sure about some of the rest of the code, but right off the bat you have an error.

You need to change
#include <calc_mean.h> //using calc_mean.h header
to
#include "calc_mean.h" //using calc_mean.h header

Topic archived. No new replies allowed.