6 years, 11 months ago.

Suppose a program includes two libraries ...

... and each of those two libraries includes the same sub-library. Will only one copy of the sub-library be loaded?

Is the MBED library a special case in this scenario?

1 Answer

6 years, 11 months ago.

If it's a very simple library with only header files (there aren't many of those) then it'll all work perfectly, it'll include things once without any problems.

For most libraries you will get linker errors complaining about duplicate symbols.

The compiler will compile each .cpp or .c file into an object file, the linker then joins these together putting the correct links between them for function calls to the other files/libraries. So when you have the same library twice it will compile any .c/.cpp file in that library twice but then the linker will get confused because when it joins everything together some of the functions are defined twice and it won't know which ones to use.

Fortunately the solution is simple, remove one of the copies of the library. As long as it's somewhere in your project anything that depends on it will still find it and work.

Mbed is not a special case, example programs should include a copy of the mbed library but other libraries should not.

Accepted Answer