Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do linkers actually do with multiply-defined `inline` functions?

Tags:

c++

c

inline

linker

In both C and C++, inline functions with external linkage can of course have multiple definitions available at link-time, the assumption being that these definitions are all (hopefully) identical. (I am of course referring to functions declared with the inline linkage specification, not to functions that the compiler or link-time-optimizer actually inlines.)

So what do common linkers typically do when they encounter multiple definitions of a function? In particular:

  • Are all definitions included in the final executable or shared-library?
  • Do all invocations of the function link against the same definition?
  • Are the answers to the above questions required by one or more of the C and C++ ISO standards, and if not, do most common platforms do the same thing?

P.S. Yes, I know C and C++ are separate languages, but they both support inline, and their compiler-output can typically be linked by the same linker (e.g. GCC's ld), so I believe there cannot be any difference between them in this aspect.

like image 437
Kyle Strand Avatar asked Feb 05 '16 21:02

Kyle Strand


People also ask

What is inline function why it is used explain using appropriate example?

The inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. Using inline functions can make your program faster because they eliminate the overhead associated with function calls.

What is the function of the __ inline?

The __inline keyword causes a function to be inlined only if you specify the optimize option. If optimize is specified, whether or not __inline is honored depends on the setting of the inline optimizer option. By default, the inline option is in effect whenever the optimizer is run.

How does inline function actually work?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Can the linker inline functions?

The linker can inline small functions in place of a branch instruction to that function. For the linker to be able to do this, the function (without the return instruction) must fit in the four bytes of the branch instruction.


2 Answers

If the function is, in fact, inlined, then there's nothing to link. It's only when, for whatever reason, the compiler decides not to expand the function inline that it has to generate an out-of-line version of the function. If the compiler generates an out-of-line version of the function for more than one translation unit you end up with more than one object file having definitions for the same "inline" function.

The out-of-line definition gets compiled into the object file, and it's marked so that the linker won't complain if there is more than one definition of that name. If there is more than one, the linker simply picks one. Usually the first one it saw, but that's not required, and if the definitions are all the same, it doesn't matter. And that's why it's undefined behavior to have two or more different definitions of the same inline function: there's no rule for which one to pick. Anything can happen.

like image 119
Pete Becker Avatar answered Oct 21 '22 08:10

Pete Becker


The linker just has to figure out how to deduplicate all the definitions. That is of course provided that any function definitions have been emitted at all; inline functions may well be inlined. But should you take the address of an inline function with external linkage, you always get the same address (cf. [dcl.fct.spec]/4).

Inline functions aren't the only construction which require linker support; templates are another, as are inline variables (in C++17).

like image 31
Kerrek SB Avatar answered Oct 21 '22 08:10

Kerrek SB