我将 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;
}
最佳答案
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/