in c++, the standard library is implemeted in template, which means that they will be instantiated in each shared libraries, there must be that two libs have the same function with the same name, for example std::vector<std::string>
, different libraries might both instantiated it, then when those libs are used in one same process, how the linker resolve the symbols, does this means that only one code in one lib is used?
if the linker use the implementation in lib a for example, when program ends and lib a is unmapped, will it cause other libs that use implementation fail?
--edit--
For anyone that might interest in this topic, I summarized relevent topic and write it down at: https://shan-weiqiang.github.io/2024/11/03/symbols-libraries-one-definition-rule.html
Start with the obvious: libraries are loaded in one order, unloaded in the reverse order, exactly so that non-cyclical dependencies can work.
Now ask the question again: how can the dynamic linker resolve a symbol so that it's not prematurely unloaded? The answer is trivial: just use the first occurrence. It will be unloaded last.
Note that most std::vector<std::string>
methods are likely to be inlined and simply won't appear in any library, precisely because they're templates. operator[]
for instance is not a whole lot more than a simple addition.