我是靠谱客的博主 机灵钥匙,最近开发中收集的这篇文章主要介绍GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

属性__attribute__((constructor)和__attribute__(((destructor))

  1. __attribute__是可以修饰多个函数的,这给当然不例外。
  2. 但是需要注意的是执行顺序,同一属性的函数执行顺序似乎不太好确定。原因的话等什么时候看看手册吧。
  3. 如果是编译C++的话,我在WSL上用gcc(gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04))编译发现无法运行,但是用Win10上装的(gcc version 4.9.2 (i686-posix-sjlj, built by strawberryperl.com project))又可以运行。至于是全局类的构造过程、析构过程、attribute((constructor)和__attribute__(((destructor))发生的顺序,无法确定。

#include <stdio.h>
__attribute__((constructor)) void before_main1()
{
    printf("before main1n");
}

__attribute__((constructor)) void before_main2()
{
    printf("before main2n");
}
__attribute__((destructor)) void after_main2()
{
    printf("after main2n");
}
__attribute__((destructor)) void after_main1()
{
    printf("after main1n");
}

int main()
{
    printf("in mainn");

    printf("before_main1:   %pn", before_main1);
    printf("before_main2:   %pn", before_main2);
    printf("after_main1:    %pn", after_main1);
    printf("after_main2:    %pn", after_main2);

    return 0;
}

gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
before main1
before main2
in main
before_main1:   0x7fe4e40b4169
before_main2:   0x7fe4e40b4180
after_main1:    0x7fe4e40b41ae
after_main2:    0x7fe4e40b4197
after main1
after main2

TDM-GCC 4.9.2 64-bit Release/Debug
before main2
before main1
in main
before_main1:   0000000000401530
before_main2:   000000000040154B
after_main1:    0000000000401581
after_main2:    0000000000401566
after main2
after main1

gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
before main1
before main2
in main
before_main1:   0x40057d
before_main2:   0x40058d
after_main1:    0x4005ad
after_main2:    0x40059d
after main1
after main2

#include<iostream>
using namespace std;

__attribute__((constructor)) void before_main1()
{
    cout<<"Before Main1"<<endl;
}
__attribute__((constructor)) void before_main2()
{
    cout<<"Before Main2"<<endl;
}
__attribute__((destructor)) void after_main1()
{
    cout<<"After Main1"<<endl;
}


class AAA{
    public:
    AAA(){
        cout<<"AAA construct"<<endl;
    }   
    ~AAA(){
        cout<<"AAA destructor" <<endl;
    }   
};
AAA A;
__attribute__((destructor)) void after_main2()
{
    cout<<"After Main2"<<endl;
}

int main()
{
    cout<<"in main"<<endl;
    return 0;
}.a.exe
AAA construct
Before Main2
Before Main1
in main
After Main1
After Main2
AAA destructor
#include<iostream>
using namespace std;

__attribute__((constructor)) void before_main1()
{
    cout<<"Before Main1"<<endl;
}
__attribute__((constructor)) void before_main2()
{
    cout<<"Before Main2"<<endl;
}
__attribute__((destructor)) void after_main1()
{
    cout<<"After Main1"<<endl;
}
__attribute__((destructor)) void after_main2()
{
    cout<<"After Main2"<<endl;
}


class AAA{
    public:
    AAA(){
        cout<<"AAA construct"<<endl;
    }   
    ~AAA(){
        cout<<"AAA destructor" <<endl;
    }   
};
AAA A;

int main()
{
    cout<<"in main"<<endl;
    return 0;
}
gcc version 4.9.2 (i686-posix-sjlj, built by strawberryperl.com project).a.exe
AAA construct
Before Main2
Before Main1
in main
After Main1
After Main2
AAA destructor


gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
Segmentation fault

最后

以上就是机灵钥匙为你收集整理的GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。的全部内容,希望文章能够帮你解决GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部