Search code examples
coptimizationinlining

inlining a function


I am developing in C using gcc in Linux.

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.

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?


Solution

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

    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

    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.

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