0
A
回答
0
从manpage:
如果
dlopen()
由于某种原因失败时,它返回NULL
。
还有一个非常广泛的例子。
例
下面的程序加载(glibc的)数学库中查找cos(3)函数的 地址,并打印的2.0余弦。所述 以下是建筑物的例子,并且运行该程序:
$ cc dlopen_demo.c -ldl $ ./a.out -0.416147
程序源
#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #include <gnu/lib-names.h> /* Defines LIBM_SO (which will be a string such as "libm.so.6") */ int main(void) { void *handle; double (*cosine)(double); char *error; handle = dlopen(LIBM_SO, RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); exit(EXIT_FAILURE); } dlerror(); /* Clear any existing error */ cosine = (double (*)(double)) dlsym(handle, "cos"); /* According to the ISO C standard, casting between function pointers and 'void *', as done above, produces undefined results. POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and proposed the following workaround: *(void **) (&cosine) = dlsym(handle, "cos"); This (clumsy) cast conforms with the ISO C standard and will avoid any compiler warnings. The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a. POSIX.1-2013) improved matters by requiring that conforming implementations support casting 'void *' to a function pointer. Nevertheless, some compilers (e.g., gcc with the '-pedantic' option) may complain about the cast used in this program. */ error = dlerror(); if (error != NULL) { fprintf(stderr, "%s\n", error); exit(EXIT_FAILURE); } printf("%f\n", (*cosine)(2.0)); dlclose(handle); exit(EXIT_SUCCESS); }
相关问题
- 1. 没有人知道如何安装greenplum
- 2. 如何知道当前PC
- 3. 如何当PID知道
- 4. Git如何知道当我没有,但打算如何重命名文件?
- 5. 有没有办法知道HTML表单当前正在提交?
- 6. 未知参考dlopen的
- 7. Calendar.getInstance()如何知道当前年份?
- 8. 有没有人知道如何在LINQ中重现NVL()函数
- 9. 有没有人知道如何getWindow()在Android上的服务?
- 10. 有没有人知道Perl验证器
- 11. 有没有预先知道字典
- 12. 有没有人知道合理的tableview?
- 13. 有没有人知道包发生器?
- 14. Elasticsearch查询 - 并没有返回值(当我知道它应该)
- 15. 没有RTLD_GLOBAL的typeinfo,共享库和dlopen()
- 16. 如何知道来电有没有视频或没有在PJSIP android
- 17. 如何知道
- 18. 如何知道在链接到Boost时何时使用.a或.so?
- 19. 如何知道哪个视图具有当前焦点
- 20. 我如何知道哪个下拉菜单有当前焦点
- 21. 如何知道当前鼠标位置有哪些元素?
- 22. SIGSEGV当P /调用dlopen
- 23. C++ - 如何知道map :: upper_bound()是否没有返回值?
- 24. 不知道如何创建没有OutOfMemory的大型数组Android
- 25. 我的视图如何知道没有DataContext的Presenter?
- 26. 队列如何知道一个线程没有指定task_done()?
- 27. 如何知道没有NSLog的指针(xcode)的值?
- 28. 没有人知道如何总结博客文章?
- 29. 如何知道viewController已经访问或没有?
- 30. 如何知道单选按钮没有被选中?