gpt4 book ai didi

Linux dlopen : can a library be "notified" when it is loaded?

转载 作者:IT王子 更新时间:2023-10-29 00:26:00 26 4
gpt4 key购买 nike

有没有办法让共享库在加载时得到“通知”?

换句话说,假设我在共享库上使用dlopen,是否有一个函数在共享库(例如main?)上自动调用(如果存在)

最佳答案

Libraries should export initialization and cleanup routines using the gcc __attribute__((constructor)) and __attribute__((destructor)) function attributes. See the gcc info pages for information on these. Constructor routines are executed before dlopen returns (or before main() is started if the library is loaded at load time). Destructor routines are executed before dlclose returns (or after exit() or completion of main() if the library is loaded at load time). The C prototypes for these functions are:

 void __attribute__ ((constructor))  my_init(void);  
void __attribute__ ((destructor)) my_fini(void);

取自http://tldp.org/HOWTO/Program-Library-HOWTO/index.html

也就是说,您只需将 __attribute__ ((constructor)) 添加到您希望在加载共享库时调用的函数。上面的文档还指出,较旧的 _ini 和 _fini 函数被认为已过时。

关于Linux dlopen : can a library be "notified" when it is loaded?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1602398/

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