[英]how to write a function pointer “pointing” to a macro which expands to the actual function?
我在庫lib.so
中有 function 我正在使用dlopen()
動態鏈接到我的應用程序
庫文件
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");
但是Failed in DBG_PRINT dlsym(): Err:Symbol not found
為以下要求定義 function 指針的正確方法是什么。
無法使用 function 指針指向宏。 function 指針只能指向函數。
但是,您可以有一個指向宏調用的 function 的指針。 像這樣:
auto fptr = &DebugPrint;
Symbol not found
這意味着動態庫中沒有該名稱的符號。
一個典型的錯誤是嘗試加載具有 C++ 語言鏈接的 function。 此類 function 的符號將被損壞,並且與 function 的名稱不匹配。
可以使用語言鏈接聲明將函數聲明為具有 C 鏈接:
extern "C"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.