Force one macro to be embedded in GCC, Clang, and Intel Compiler?

I have a function that I need to embed in a hard loop in C ++ 11

I want the function to be implemented in a separate file from the header and still force it to be embedded in its entire scope. In addition, I want to compile with both clang, GCC, and the Intel compiler.

To formulate a requirement. I am looking for a macro that will allow me to do something like:

#define force_inline <something here> 

In the headers:

 force_inline void foo(); 

And I have to do this in the implementation file:

 void foo() {... Code.. } 

To be clear, I don't want to put code in my headers. I want them to contain only function declarations.

Is there a way to achieve inlining with a macro that works on all of these compilers?

The best solution I've got so far is this macro:

 #define forceinline inline __attribute__((always-inline)) 

ICC seems to need inline (which has nothing to do with code embedding) and a full implementation in the header to guarantee function embedding.

PS: Yes, I measured my performance, and I know that itโ€™s faster to have a built-in function than not. And no, the compiler does not do this for me.

+7
c ++ gcc c ++ 11 clang icc
source share
1 answer

By definition, a built-in function has its own code included (by the compiler) in every place that it calls.

This means that the compiler must have access to the function code when it creates the compilation block of the callers. This is technically feasible, but not easy and difficult to scale. Please note: if you distribute only your header and library, this means that the compiler using your component must extract the code from the library!

If it is implemented by the compiler, this means that the code from the header is not useful to you, as well as for ordinary functions (changing the CPP file will require recompiling all the files depending on the header, even if it wasnโ€™t.) The only advantage is to have a header, which does not contain code (which is still good).

The only solution currently available that I know of is to optimize the GCC connection time, so it doesn't meet your requirement to work with clang and icc: https://gcc.gnu.org/wiki/LinkTimeOptimization Something similar may exist for those compilers with certain options that would have to run code that depends on the compiler to support it.

Solution 1: You can save a โ€œpure classโ€ without any implementation in the header and put inline functions outside the class (but still inside the header). You still have the definitions in the header, but it is clearly separate from the declaration, so this impairs readability. (This is my preferred solution personally)

Solution 2: Another way to mitigate your problem, if you really want only declarations in the header, is to separate your code from 3 files instead of 2: - an .h file containing only an interface - an .i file containing built-in functions and included with the .h file is a .cpp / .cc file containing the rest of the code

Obviously, this also has flaws, as your code is now split into two different files ...

Please tell me if you see a problem that I missed and what you see in these sentences.

+1
source share

All Articles