内联函数 [英] inlining a function

查看:90
本文介绍了内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Linux中使用gcc进行C语言开发.

I am developing in C using gcc in Linux.

我正在通过以下方式在.H和.C文件中组织小型函数

I am organizing my small functions in .H and .C files in the following way

      // .H file
      extern int my_function( int val );

      // .C file
      inline int my_function( int val ){
          // my job..very short!
      }

这些函数很小,因此它们非常适合内联,但是我不知道它们是否肯定.

These functions are small so they are very good candidates to be inlined but I don't know if they will be for sure.

我对我整理文件的方式表示怀疑,我当时想将所有功能直接内联到.h文件而不使用.C文件可能会更好. 您能帮我澄清一下疑问吗?

I have doubt about the way I cam organizing my file and I was thinking that probably it would be better to have all my functions inlined directly into the .h file without .C file. Can you help me a bit in clarifying my doubt?

推荐答案

首先,请注意,您可以通过添加-finline-functions选项告诉 gcc 尝试将小型函数内联到其调用程序中

First of all, note that you can tell gcc to try and inline small functions into their callers by adding the -finline-functions option.

如果您怀疑函数实际上会被内联,则可以使用-Winline

If you have any doubt that your function will actually be inlined, you can ask gcc to warn you about uninlinable function, by using -Winline

在这里,由于您的函数都被声明为externinline,它们将被内联 ,因为inline定义仅用于内联.在这种特殊情况下,该函数将根本无法单独编译.

Here, as your functions are declared both extern and inline, they will be inlined as the inline definition will only be used for inlining. In this particular situation the function will not be compiled on its own at all.

阅读 http://gcc.gnu.org/onlinedocs/gcc/Inline.html 以获得更多详细信息.

Read http://gcc.gnu.org/onlinedocs/gcc/Inline.html for further details.

这篇关于内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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