Inline all the functions in C code with gcc

让人想犯罪 __ 提交于 2019-12-11 04:18:59

问题


I have many C programs without recursion. I want to get the program without user-defined function but the main function. GCC can do the inline but that's in IR level so I can't get C code .

SOURCE:

int calc(int a , int b)
{
    a=a+b-2;
    return a ;
}

int main()
{
    int x=4,y=7;
    x=calc(x,y);
    return 0 ;
}

TARGET:

int main()
{
    int x=4,y=7;
    int calc_A=x,calc_B=y;
    calc_A=calc_A+calc_B-2;
    x=calc_A;
    return 0 ;
}

回答1:


There is a function attribute provided by gcc, called always_inline.

Usage:

int add(int arg1, int arg2)__attribute__((always_inline)); // prototype
int add(int arg1, int arg2){
    return arg1+arg2;
}

However, you would have to manually attach this attribute to every function.

I am still assuming that all your functions follow rules which are necessary to be inlined. e.g. no goto, recursion, etc.



来源:https://stackoverflow.com/questions/13970806/inline-all-the-functions-in-c-code-with-gcc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!