使用gcc内联C代码中的所有功能 [英] Inline all the functions in C code with gcc

查看:132
本文介绍了使用gcc内联C代码中的所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多C程序没有递归。
我想获得没有用户定义函数但主函数的程序。
GCC可以执行内嵌,但这是在IR级别,所以我可以'code $ c $>

消息来源:

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

int main()
{
int x = 4,y = 7;
x = calc(x,y);
返回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;
返回0;


解决方案

提供了一个函数属性由gcc调用,名为 always_inline $ b

用法: > int add(int arg1,int arg2)__ attribute __((always_inline)); //原型
int add(int arg1,int arg2){
return arg1 + arg2;
}

但是,您必须手动将此属性附加到每个函数。 p>

我仍然假设您的所有函数遵循需要内联的规则。例如没有转到,递归等。


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

这篇关于使用gcc内联C代码中的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆