简体   繁体   中英

Inline all the functions in C code with gcc

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 ;
}

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. eg no goto, recursion, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM