我在库lib.so
中有一个函数,我正在使用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: %sn", 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");
但我在运行时出错Failed in DBG_PRINT dlsym(): Err:Symbol not found
对于以下需求,定义函数指针的正确方法是什么。
没有办法用函数指针指向宏。函数指针只能指向函数。
但是,您可以有一个指向宏调用的函数的指针。像这样:
auto fptr = &DebugPrint;
Symbol not found
这意味着动态库中没有该名称的符号。
一个典型的错误是试图加载具有C++语言链接的函数。这样的函数的符号将被损坏,并且与函数的名称不匹配。
函数可以通过语言链接声明声明为具有C链接:
extern "C"