4

I have a file funcs.h where I have the declaration of function:

inline void some_func( void ) __attribute__((always_inline));

Then I have a file funcs.c where I have the implementation of the function:

inline void some_func( void ) { … }

Then in my main file main.c I #include the funcs.h and try to use some_func() somewhere in the code. However when I compile my program and look at the binary file, the function appears to be compiled as a regular separate function, and called just like any other regular function, instead of being embedded as inline.

Why is that happening, and is there a way to force actual inlining into this? (Except the option of just using #define macros instead of functions, of course.)

5
  • 4
    How is the compiler supposed to inline a definition that is found after compilation? Commented Jun 18, 2014 at 15:34
  • 4
    You will have to turn on link-time optimization (-flto switch). Commented Jun 18, 2014 at 15:36
  • @ghostofstandardspast not the compiler, the linker. Commented Jun 18, 2014 at 15:36
  • @user3477950, That's a good point I guess. I remember Microsoft saying something about the optimizations the linker can do. Commented Jun 18, 2014 at 15:37
  • @user3477950 simple testing shows that even a+b function isn't inlined with gcc 4.8 and -flto. Moreover, using this attribute produces compile error, since compiler knows for sure that it can't inline, while programmer insists that it should. Commented Jun 18, 2014 at 15:50

1 Answer 1

4

Put the implementations is the header. If they're not available in the translation unit in which you intend to do the inlining, you'll be out of luck. The linker (well, a traditional linker) can't do anything about that for you.

Sign up to request clarification or add additional context in comments.

4 Comments

Most modern toolchains support link-time optimization, relying on that would probably be a better idea that dumping huge pieces of definitions in headers.
@user3477950: Yes, though relying on compiler optimization makes the force_inline-attribute superfluous.
@Deduplicator …which tells me that one should probably leave low-level optimizations to the compiler, especially nowadays.
In header file, in case of C and not C++, it should be static inline. inline itself does not flag function as non-exportable (but in C++ it does).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.