Search code examples
c++dlopen

How do i call an external function?


I have code i'm trying to write, I have a void function, info.

void info(char *,char *);

This, I'm trying to call from my main function in a separate file. I want to use dlopen to open a so file. How would i call the function: info. From my other file?

I'm trying to use

info("testing: ","Success");

I get an undefined reference error on my info function.


Solution

  • The usual path is something like this:

    /* Set up a typedef for the function pointer to make the code nicer */
    tyepdef void(*Info_ptr)(char*, char*);
    /* Get the function, lib must be the dlopened library.*/
    Info_ptr info;
    info = (Info_ptr)dlsym( lib, "info");
    /* Use the function pointer */     
    (*info)("testing: ", "Success");
    

    Take al ook here for a tute: http://tldp.org/HOWTO/html_single/C++-dlopen/