Automatically executed functions when loading shared libraries
来源:互联网 发布:对美工电商有什么了解 编辑:程序博客网 时间:2024/06/11 18:31
http://stackoverflow.com/questions/9759880/automatically-executed-functions-when-loading-shared-libraries
You can define an on-load function for a linux library using the .init
mechanism. This is the same as specifying the load-time entry point for a binary (e.g. using something other than main as the entry point for a program).
When linking using ld
directly you use the:
-init <function name>
or if you're using cc/gcc to link, you use:
-Wl,-init,<function name>
This is at it's most simple level.
Edit For destructors/finalizers, you use the .fini
mechanism. This operates in the same manner as the init option, and you use:
-fini <function name>
when invoking ld
. Availability is limited to the -init
option on the Mac OSX platform.
You should also be able to use the __attribute__((constructor))
syntax for gcc:
static void con() __attribute__((constructor));void con() { printf("I'm a constructor\n");}
Which is probably a more portable way rather than screwing with the linker options. All constructors should be invoked at load-time, but don't depend on the order of their initialization, that leads to insanity and unreproducible bugs that cost time and effort to debug.
Edit 2 The use of the __attribute__((constructor))/__attribute__((destructor))
semantic is the most preferable mechanism for the C/C++ programming language.
For the D
programming language you should really use the static module constructor/destructor:
static this() { printf("static this for mymodule\n");}static ~this() { printf("static ~this for mymodule\n");}
Or the static class constructor:
class Foo { static this() { printf("static this for Foo\n"); }}
This is strongly hinted at in the writing win32 DLLS and in the language specification relating to static constructors/destructors.
- Automatically executed functions when loading shared libraries
- Loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- error while loading shared libraries
- php 下载放在服务器里面的pdf文件代码
- Java:单例模式的七种写法
- 关于ffmpeg命令行的ss参数
- CocoaPod代码库创建,配置Spec踩过的坑
- xshell vim中文乱码
- Automatically executed functions when loading shared libraries
- 2016 大数据版图
- 春节期间,我教你:怎样晒朋友圈才安全?
- ProgressBar美化
- Jackson 动态过滤属性
- poj 1691
- js实现换肤
- 使用JSoup+CSSPath采集和讯网人物信息
- Eclipse抽疯治愈大招