5

i have ended putting many little small inline function in a header file which i include in many compilation units, the code is correctly inlined by the compiler and the program works like a charm.

but now the header file is something very unusual (for being a header file), to make it more readable i thought to do something like this:

#ifndef MY_HEADER_H
#define MU_HEADER_H

static inline 
void my_fnct (my_param a);

#include "my_header.inline.c"

#endif

and the file my_header.inline.c will be like:

static inline 
void my_fnct (my_param a)
{
    // .. my code ..
}

then, when i want these functions i just include the header file.

my question is: there is a better way to accomplish this without filling a header file with too much code? or i can do this and expect other developers to understand this code without problems?

user975413
  • 51
  • 2
  • 1
    What's the problem with having inline functions in the header, again? inline functions should be very short, one-liners, really. – littleadv Mar 25 '12 at 08:27
  • 1
    As for code performance, the only thing how you can see if this matters is measure. What is the slowdown of your compiler with all the stuff included? My experience is that such things are negligible with modern compilers. And not worth the trouble of having hundreds of configuration options that you easily mix up. – Jens Gustedt Mar 25 '12 at 08:52

2 Answers2

1

No you cannot. However, you might use the Link Time optimization feature of GCC (then some calls might be inlined, even if the function is not declared inline and not available in headers for every compilation unit), e.g. compile and link with gcc -flto (this requires a recent GCC compiler, e.g. 4.6 at least and slows down the build time).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

A practice that I don't personally like is to extract inline functions into a separate file with the .inl extension. This is just convention and you can name the file anything you like. I have an IDE that folds code so I can just hide the stuff I don't want to see rather than have a separate file.

Ed J
  • 101
  • 2