Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

header full of inline functions, can i move the code outside header file and still inline everything?

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?

like image 613
user975413 Avatar asked Nov 04 '22 03:11

user975413


2 Answers

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).

like image 70
Basile Starynkevitch Avatar answered Nov 09 '22 07:11

Basile Starynkevitch


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.

like image 28
Ed J Avatar answered Nov 09 '22 08:11

Ed J