gcc下__attribute__ ((constructor))和__attribute__ ((destructor))的使用
2010-12-01 17:16
323 查看
__attribute__ ((constructor))指定的函数在共享库loading的时候调用,__attribute__ ((destructor))指定的函数在共享库unloading的时候调用。
1. 编写源码文件ktest.c如下.
#include <stdio.h>
__attribute__ ((constructor)) static void ktest_init(void);
__attribute__ ((destructor)) static void ktest_deinit(void);
void ktest_init(void)
{
printf("call ktest init./n");
}
void ktest_deinit(void)
{
printf("call ktest deinit./n");
}
void test1()
{
printf("call test1./n");
}
void test2()
{
printf("call test2./n");
}
void test3()
{
printf("call test3./n");
}
2. 编译为共享库libktest.so
gcc -fPIC -c ktest.c ### produce ktest.o.
gcc -shared -o libktest.so ktest.o ### produce libktes.so
3. 编写库调用文件calllib.c
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h> /* dlopen, dlsym, dlclose */
int main(int argc, char **argv)
{
test1();
test2();
test3();
/* below only for testing dynamic linking loader */
void *handle;
void (*test)();
char *error;
handle = dlopen ("/path_to_lib/libktest.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
test = dlsym(handle, "test2");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
(*test)();
dlclose(handle);
return 0;
}
4. 编译calllib运行, 将可以看到ktest_init() 和ktest_deinit()已被调用.
gcc calllib.c -o calllib -ldl -L./ -lktest ### produce calllib
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH && ./calllib
call ktest init.
call test1.
call test2.
call test3.
call test2.
call ktest deinit.
src: http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html#INIT-AND-CLEANUP
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
http://unixwiz.net/techtips/gnu-c-attributes.html
1. 编写源码文件ktest.c如下.
#include <stdio.h>
__attribute__ ((constructor)) static void ktest_init(void);
__attribute__ ((destructor)) static void ktest_deinit(void);
void ktest_init(void)
{
printf("call ktest init./n");
}
void ktest_deinit(void)
{
printf("call ktest deinit./n");
}
void test1()
{
printf("call test1./n");
}
void test2()
{
printf("call test2./n");
}
void test3()
{
printf("call test3./n");
}
2. 编译为共享库libktest.so
gcc -fPIC -c ktest.c ### produce ktest.o.
gcc -shared -o libktest.so ktest.o ### produce libktes.so
3. 编写库调用文件calllib.c
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h> /* dlopen, dlsym, dlclose */
int main(int argc, char **argv)
{
test1();
test2();
test3();
/* below only for testing dynamic linking loader */
void *handle;
void (*test)();
char *error;
handle = dlopen ("/path_to_lib/libktest.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
test = dlsym(handle, "test2");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
(*test)();
dlclose(handle);
return 0;
}
4. 编译calllib运行, 将可以看到ktest_init() 和ktest_deinit()已被调用.
gcc calllib.c -o calllib -ldl -L./ -lktest ### produce calllib
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH && ./calllib
call ktest init.
call test1.
call test2.
call test3.
call test2.
call ktest deinit.
src: http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html#INIT-AND-CLEANUP
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
http://unixwiz.net/techtips/gnu-c-attributes.html
相关文章推荐
- gcc下__attribute__ ((constructor))和__attribute__ ((destructor))的使用
- 如何在C语言中使用constructor和destructor,gcc环境
- GCC __attribute__((constructor)|(destructor))
- GCC的__attribute__ ((constructor))和__attribute__ ((destructor))
- gcc __attribute__关键字举例之constructor/destructor
- gcc 函数属性 __attribute__ ((constructor))使用
- 如何在C语言中使用constructor和destructor,gcc环境
- GCC的__attribute__ ((constructor))和__attribute__ ((destructor))
- GCC __attribute__((constructor和(destructor)) (Cygwin)
- 如何在C语言中使用constructor和destructor,gcc环境
- GCC __attribute__((constructor)|(destructor))
- GCC __attribute__((constructor)|(destructor))
- GCC中 __attribute__((constructor)|(destructor))
- gcc的__attribute__ ((constructor))和__attribute__ ((destructor))
- GCC __attribute__((constructor)|(destructor))
- gcc __attribute__((section("section_name"))) 使用方法
- __attribute__中constructor和destructor
- GCC使用__attribute__关键字
- gcc中的constructor属性和destructor属性
- GNU C - 一个别致的HelloWorld程序 引申到: __attribute__((constructor)|(destructor)(PRIORITY))