c++ - gcc -finline-functions 行为?

标签 c++ gcc inline

我将 gcc 与 -finline-functions 优化一起用于发布构建。为了对抗代码膨胀,因为我在嵌入式系统上工作,我想说不要内联特定功能。显而易见的方法是通过函数属性,即 attribute(noinline)。问题是当我打开作为 -O3 开关一部分的全局 -finline-functions 优化时,这似乎不起作用。

它也与它被模板化有关,因为同一函数的非模板化版本没有像预期的那样被内联。

有人知道当这个全局开关打开时如何控制内联吗?

代码如下:

#include <cstdlib>
#include <iostream>

using namespace std;

class Base
{
public:

    template<typename _Type_>
    static _Type_ fooT( _Type_ x, _Type_ y ) __attribute__ (( noinline ));
};

template<typename _Type_>
_Type_ Base::fooT( _Type_ x, _Type_ y )
{
    asm("");
    return x + y;
}


int main(int argc, char *argv[])
{
    int test = Base::fooT( 1, 2 );
    printf( "test = %d\n", test );

    system("PAUSE");
    return EXIT_SUCCESS;
}

最佳答案

docs for GCC's noinline 说:

This function attribute prevents a function from being considered for inlining. If the function does not have side-effects, there are optimizations other than inlining that causes function calls to be optimized away, although the function call is live. To keep such calls from being optimized away, put

     asm ("");

(see Extended Asm) in the called function, to serve as a special side-effect

我认为自从 Base::fooT<> 以来,您可能遇到的情况是函数没有副作用,GCC 正在调用上述未指定的其他优化。

关于c++ - gcc -finline-functions 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2451316/

相关文章:

c++ - 如何在 DirectX 11 应用程序中提供选择图形适配器的选项?

c - GCC 编译 (ld) 问题的最后一步

c# - 在电子邮件中发送内联图像

c - 在 makefile 中的何处添加 -lm 标志?

c - 后缀或操作数对于 gcc 的 `move' 无效

javascript - 将 CSS/Javascript 内联到 HTML 文件中

html - 导航栏不显示内联

c++ - VARIADIC MACRO 编译错误

c++ - 类对象为 float 类型

c++ - UUID生成器问题