GCC属性对嵌套函数的影响

GCC属性对嵌套函数的影响,c,gcc,compiler-construction,c99,function-attributes,C,Gcc,Compiler Construction,C99,Function Attributes,只能为函数声明(而不是定义)指定函数属性。 所以,我不能为嵌套函数指定属性。 例如: //invalid line. hot_nested_function is invisible outside "some_function" void hot_nested_function() __attribute__ ((hot)); //valid attribute for declaration int some_function() __attribute__ ((hot)); int s

只能为函数声明(而不是定义)指定函数属性。 所以,我不能为嵌套函数指定属性。 例如:

//invalid line. hot_nested_function is invisible outside "some_function"
void hot_nested_function() __attribute__ ((hot));

//valid attribute for declaration
int some_function() __attribute__ ((hot));

int some_function(){
    void hot_nested_function(){
         //time critical code
    }
    for( int i=0; i<REPEAT_MANY_TIMES;i++)
        hot_nested_function();
}
//无效行。热嵌套函数在“某些函数”外部不可见
void hot_嵌套_函数()_属性_((hot));
//声明的有效属性
int some_function()uu属性_uu((热));
int some_函数(){
void hot_嵌套_函数(){
//时间关键代码
}
对于(int i=0;i
最后一个只能用于原型,但前两个不能

另一种方法是

int some_function(){
    auto void hot_nested_function() __attribute__ ((hot));

    void hot_nested_function(){
         //time critical code
    }

    for( int i=0; i<REPEAT_MANY_TIMES;i++)
        hot_nested_function();
}
int some_函数(){
自动作废hot_嵌套函数()_属性__((hot));
void hot_嵌套_函数(){
//时间关键代码
}

对于(int i=0;iDid您真的尝试了吗?它似乎对我有用。您使用的是什么版本的gcc以及什么命令行开关?@PaulR
gcc版本4.8.1 20130909[gcc-4_8-branch revision 202388]
编译时使用:
gcc-std=gnu99-O2 file.c
它当然会编译-这是正确的c代码(但逻辑上不正确-hot_嵌套函数声明为全局函数,而不是嵌套函数)。嵌套函数在C中不推荐使用,已经有一段时间了。如果发现支持处于某种bitrot状态,我不会感到太惊讶。我认为您的gcc命令行中缺少了
-fnested函数
。请添加解释,以便其他有相同问题或出于学习目的阅读的人能够理解更改e、 谢谢。谢谢,也许我错过了这部分文档。但问题的第二部分呢?对“some_function”有“attribute”吗?对嵌套函数有什么影响吗?@AlexanderSannikov抱歉,我不知道。不过,更新了答案并提供了更多的信息。可以通过查看反汇编来测试它-但同样,会发生什么n在不同的gcc版本中。@keltar,无论如何,非常感谢。我将尝试反汇编不同的版本。
__attribute__ ((...)) returntype functionname(...)
returntype __attribute__ ((...)) functionname(...)
returntype functionname(...) __attribute__ ((...))
int some_function(){
    auto void hot_nested_function() __attribute__ ((hot));

    void hot_nested_function(){
         //time critical code
    }

    for( int i=0; i<REPEAT_MANY_TIMES;i++)
        hot_nested_function();
}