-1

I have function in library lib.so which I'm linking to my application dynamically using dlopen()

lib.h

void DebugPrint( unsigned char logLevel,
                 const char     *programName,
                 const char     *format,
                 ... );

#define DBG_PRINT(logLvl, format,  ...)             \
        DebugPrint(logLvl,TL_MODULE_NAME, format, ## __VA_ARGS__) 

myapp.c

void (*DBG_PRINT_ptr)( unsigned char logLevel,
                 const char     *programName,
                 const char     *format,
                 ... );

    void *handle = NULL;
    bool ret = RESULT_SUCCESS;

    /* Open Shared Lib into the same Process */
    /* LDRA_INSPECTED 496 S */
    handle = dlopen(lib.so, RTLD_NOW);
    if (NULL == handle)
    {
        /* fail to load the library */
        LLOG_error(" dlopen Error to open handle: %s\n", dlerror());
        ret = RESULT_FAILURE;
    }
        if(RESULT_SUCCESS == ret)
    {
              DBG_PRINT_ptr = dlsym(handle, "DebugPrint");

        if( DBG_PRINT_ptr  == NULL)
        {
           LLOG_error("Failed in DBG_PRINT dlsym(): Err:%s", dlerror());
           dlclose(handle);
           ret = RESULT_FAILURE;
        }
   }
(void)DBG_PRINT_ptr  ("loglevel1","dd","printval");

but I'm getting error while runtime Failed in DBG_PRINT dlsym(): Err:Symbol not found

what is the correct way to define function pointer for the following requirement.

rici
  • 234,347
  • 28
  • 237
  • 341

1 Answers1

1

There is no way to point to a macro with a function pointer. Only functions can be pointed by a function pointer.

You can have a pointer to the function that the macro calls however. Like this:

auto fptr = &DebugPrint;

Symbol not found

This means that there is no symbol by that name in the dynamic library.

One typical mistake is to attempt to load a function which has C++ language linkage. The symbol of such function will be mangled and won't match the name of the function.

Functions can be declared to have C linkage with a language linkage declaration:

extern "C"
eerorika
  • 232,697
  • 12
  • 197
  • 326