可以通过"objdump -S 程序名" 命令来查看ELF中的汇编代码,可以找到这两个函数
_init _fini 函数是给编译器使用的,可以通过下面两个方法进行使用
newtest.c
#include <stdio.h>
__attribute ((constructor)) void GNUC_init(void)
{
printf("%s/n", __func__);
}
__attribute ((destructor)) void GNUC_fini(void)
{
printf("%s/n", __func__);
}
int test(int n)
{
return n;
}
test.cpp:
#include <stdio.h>
class InitGNUCPP
{
public:
InitGNUCPP()
{
printf("%s/n", __func__);
}
~InitGNUCPP()
{
printf("%s/n", __func__);
}
};
static InitFini aInitFini;
main.c:
int test(int n);
int main(int argc, char* argv[])
{
test(1);
return 0;
}
g++ -O0 -g3 -Wall -c -fmessage-length=0 -otest.o ../test.cpp
gcc -O0 -g3 -Wall -c -fmessage-length=0 -onewtest.o ../newtest.c
gcc -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ../main.c
g++ -oinitFini.exe test.o newtest.o main.o