1

Bit of a theoretical question (instead of a specific example) I thought I had understood this, but now I am having doubts again...

Scenario:

  1. We have an common class called bob and its a header only implementation.

  2. We have a library (lib1) that uses bob (such that bob is defined in the compiled library).

  3. We also have an application (exe1) that uses bob (such that bob is defined in the compiled executable).

Now there is (afaik) a definition of bob in both lib1 and exe1. What happens if I try to link lib1 into exe1... is that possible?

Is there any difference if I make the class bob into a .cpp and .hpp pair of files and compile the cpp file in both the library and the exectuable - or is that basically the same thing?

My question really relates to what definitions are within the two binaries and what does the linker really do about all this? (the code being trivial). My understanding is that this should always cause a redefinition of bob error, but I don't seem to be seeing this.

This could mean I am doing somthing wrong - so I wanted to confirm with you guys if what I believe is correct?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • *"header only implementation"* - So the exact same tokens may and are even meant to appear more than once without preventing the linkage of separate object files. Why is it a shock that it still works when linking a library with an executable? – StoryTeller - Unslander Monica Jun 26 '19 at 09:36
  • 1
    If `Bob` is header only and everything is built with the exactly same tool-chain everything will work. – Richard Critten Jun 26 '19 at 09:36
  • 1
    The same as if you compile one .cpp twice with different names: probably a lot of linker errors. – Zefick Jun 26 '19 at 09:40
  • @RichardCritten Pretty sure this would be fine if the functions are inlined (no need for header only)- https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method – UKMonkey Jun 26 '19 at 09:43
  • if it is a class with inlined member functions, everything should be fine; see any std templatized structure, you may include them in many different files with no problem). You might have problems if you add functions without inline or static specifiers. – amlucas Jun 26 '19 at 09:44
  • 1
    @Zelick - incorrect. A correct "header only implementation" will have all functions inline, so they will not break the one-definition rule. – Peter Jun 26 '19 at 09:44
  • is it a dynamic library or static library? – Jarod42 Jun 26 '19 at 10:02
  • @all thanks for the comments (its a shared lib), I had not used `inline` in this case, but I can fix that up. However, it seems that (as Maxims answer suggests) this would not be a problem either way (inline'd or not) - although I will inline them anyway as it appears to be good practise – code_fodder Jun 26 '19 at 12:37

1 Answers1

2

Now there is (afaik) a definition of bob in both lib1 and exe1. What happens if I try to link lib1 into exe1... is that possible?

The linker will use the definition of bob from exe and ignore the one from lib1 (regardless whether the library is static or dynamic). This is because linkers search libraries for unresolved symbols only.

However, it you try to link 2 object files both containing definitions of bob then you get multiple symbol definition error, because the linker pulls in the entire contents of an object file (unlike libraries).

The above applies to non-inline symbols with external linkage only.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271