1

As far as I know it's impossible to have a function declared as "inline" in a lib file and have that function "magically inlined" into a caller function into another project (since linking is not the same as compiling and the latter happens before).

How could I inline a function when having multiple functions (into multiple libraries) that have the same declaration but different definition?

e.g.

obj1.lib

void function1() { printf("Hi"); }

obj2.lib

void function1() {printf("whatsup?"); }

main.cpp

void function1();

int main()
{
  function1(); // I'd like to be able to inline this, I can steer the linking against obj1 or obj2, but I can't inline this one
}
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 2
    Do you mean in the optimization sense of inlining? You'll need to turn on link-time-optimization, which in GCC is `-flto` and MSVC++ is called Whole Program Optimization. This won't guarantee inlining, but it will at least make it possible. – Sam Cristall Dec 19 '13 at 15:52

4 Answers4

2

To inline a function from an object (or a library) file you'll need this object file be compiled with link-time optimization (LTO). See Inlining functions from object files for more details.

Community
  • 1
  • 1
vitaut
  • 49,672
  • 25
  • 199
  • 336
1

Even if you inline functions, they always have to have the same definition: having different defintions for the same entity in a C++ program is a violation of the one definition rule (ODR) specified in 3.2 [basic.def.odr]. ODR violations are often not detected by compilers and linkers and tend to result in rather weird problems.

You'll need to make sure the functions are different, e.g., using one of these techniques:

  1. Give them a different name.
  2. Put the functions into a namespace.
  3. Give the functions a different signature.
  4. Make the static to have them visible only in the given translation units.
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

The simplest you can do is to give the functions different names.

If you want built-time selection of a function with a given name, that has 2 or more different implementations, and you want to support machine code inlining of that function, then declare it as inline, which requires also supplying the implementation in each variant's header, and use include and lib paths to select the relevant header (for compilation) and lib (for linking) – they'd better match. As with any inline function this doesn't guarantee machine code inlining. With respect to machine code inlining inline is just a hint (it's guaranteed effect is about permitting a definition in each translation unit, and requiring such a definition in each translation unit where it's used).

How to use include and lib paths depends on your toolchain.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Edited in response to Sam Cristall's comment.

If you mean "inline at compile time" then:

In order to use a library you need to include the header file(s) associated with that library. If the desired function is declared inline in that header and the function definition (the body of the function) is available, then the compiler will (at it's discretion) inline the function. Otherwise it won't.

If you mean "inline at link time" (an unfortunate overloading of the word "inline") than see the other answers

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • Sometimes it feels like there needs to be some language development around the inlining ambiguity. There's a few questions a week that conflate "inlining" in the C++ definition sense, and "inlining" in the LTO sense. – Sam Cristall Dec 19 '13 at 15:59
  • Ah -- I was unfamiliar with that overloading of the word "inline" It seems like an unfortunate choice, but google does find both usages. Thanks for pointing this out, Sam. – Dale Wilson Dec 19 '13 at 16:01