c - 如何编写函数指针“指向”扩展为实际函数的宏?
问题描述
我在库lib.so
中有函数,我正在使用动态链接到我的应用程序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__)
我的应用程序
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
为以下要求定义函数指针的正确方法是什么。
解决方案
没有办法用函数指针指向宏。函数指针只能指向函数。
但是,您可以有一个指向宏调用的函数的指针。像这样:
auto fptr = &DebugPrint;
Symbol not found
这意味着动态库中没有该名称的符号。
一个典型的错误是尝试加载具有 C++ 语言链接的函数。此类函数的符号将被破坏,并且与函数的名称不匹配。
可以通过语言链接声明将函数声明为具有 C 链接:
extern "C"
推荐阅读
- python - 为什么不显示 OGR 层?
- wordpress - Wordpress Mailpoet 3 不在 Outlook 中显示图像
- mysql - 通过删除 MySQL 中的记录,通过 Logstash 删除 Elasticsearch 中的文档
- rabbitmq - rabbitmq arm64 容器访问问题
- javascript - 我可以在角度项目中使用散列(#)和非散列(#)路由吗
- azure - 定期删除 Azure 订阅中的资源组
- flutter - flutter,如何判断树中是否有另一个widget
- typescript - 父类上的 TypeScript 工厂方法返回子类类型
- javascript - 具有特殊要求的 Javascript 电子邮件验证
- javascript - javascript 数组 8>50 返回真