gpt4 book ai didi

c++ - 使本地 c 库函数可全局访问

转载 作者:行者123 更新时间:2023-11-28 07:43:23 27 4
gpt4 key购买 nike

我正在使用一个名为 GLC 的 C 库以编程方式记录我的 OpenGL 缓冲区。GLC 监听按键,这并不是一个真正好的以编程方式触发的解决方案。

因此我想通过我软件中的函数调用从 GLC 执行记录。我的 C++ 软件正在链接到包含所需函数 start_capture() 的库。通过 nm 我可以看到这个函数是局部的,用小写的 t 标记。

因为它必须是全局的才能在我的软件中访问它,所以我想重新编译库(我已经完成了)。但是我不知道要更改什么才能使其易于访问....

这是来自 start_capture() 的声明,在头文件中 lib.h

...
__PRIVATE int start_capture(); // No idea where the __PRIVATE is coming from
...

这是 main.cstart_capture() 函数的定义/实现:

int start_capture()
...
return ret;
}

这是我用来获取函数的 dlopen:

void *handle_so;
void (*start_capture_custom)();
char *error_so;
handle_so = dlopen("/home/jrick/fuerte_workspace/sandbox/Bag2Film/helper/libglc-hook.so", RTLD_LAZY);
if (!handle_so)
{
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
start_capture_custom = (void (*)())dlsym(handle_so, "start_capture");
if ((error_so = dlerror()) != NULL)
{
fprintf(stderr, "%s\n", error_so);
exit(1);
}
start_capture_custom();
dlclose(handle_so);
start_capture();

那么我应该更改什么才能通过库文件访问它?

我希望这足以说明问题。如果没有,我会尽快回答。

最佳答案

__PRIVATE 是 GCC 扩展的 #define 来隐藏一个符号。参见 https://github.com/nullkey/glc/blob/master/src/glc/common/glc.h#L60对于定义和 http://gcc.gnu.org/wiki/Visibility有关 GCC 扩展的更多信息。

https://stackoverflow.com/a/12011284/2146478提供了一种无需重新编译即可取消隐藏符号的解决方案。你会想做这样的事情:

$ objcopy --globalize-symbol=start_capture /path/to/your/lib.a /path/to/new/lib.a

关于c++ - 使本地 c 库函数可全局访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15387031/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com