Here program calls into function one, which is in library A. That calls into function two, which is in library B. Finally that calls into function three, which is back in library A again.
Let's assume that we use the following linker line to build the final executable:
gcc -o program prog.o liba.a libb.a
Because linkers were originally designed in the 70s, they are optimized for minimal resource usage. In this particular case the linker will first process the object file and then library A. It will detect that function one is used so it will take that function's implementation and then throw the rest of library A away. It will then process library B, take function two in the final program and note that function three is also needed. Because library A was thrown away the linker can not find three and errors out. The fix to this is to specify A twice on the command line.
This is how everyone has been told things work and if you search the Internet you will find many pages explaining this and how to set it up linker command lines correctly.
But is this what actually happens?
Let's start with Visual Studio
Suppose you were to do this in Visual Studio. What do you think would happen? There are four different possiblities:
- Linking fails with missing symbol three.
- Linking succeeds and program works.
- Either 1. or 2. happens, but there is not enough information to tell which.
- Linking succeeds but the final executable does not run.
The correct answer is 2. Visual Studio's linker is smart, keeps all specified libraries open and uses them to resolve symbols. This means that you don't have to add any library on the command line twice.
Onwards to macOS
Here we have the same question as above but using macOS's default LLD linker. The choices are also the same as above.
The correct answer is also 2. LLD keeps symbols around just like Visual Studio.
What about Linux?
What happens if you do the same thing on Linux using the default GNU linker? The choices are again the same as above.
Most people would probably guess that the correct answer here is 1. But it's not. What actually happens is 3. That is, the linking can either succeed or fail depending on external circumstances.
The difference here is whether functions one and three are defined in the same source file (and thus end up in the same object file) or not. If they are in the same source file, then linking will succeed and if they are in separate files, then it fails. This would indicate that the internal implementation of GNU ld does not work at the symbol level but instead just copies object files out from the AR archive wholesale if any of their symbols are used.
What does this mean?
For example it means that if you build your targets with unity builds, their entire symbol resolution logic changes. This is probably quite rare but can be extremely confusing when it happens. You might also have a fully working build, which breaks if you move a function from one file to another. This is a thing that really should not happen but when it does things get very confusing.
The bigger issue here is that symbol resolution works differently on different platforms. Normally this should not be an issue because symbol names must be unique (or they must be weak symbols but let's not go there) or the behaviour is undefined. It does, however, place a big burden on cross platform projects and build systems because you need to have very complex logic in place if you wish to deduplicate linker flags. This is a fairly common occurrance even if you don't have circular dependencies. For example when building GStreamer with Meson some time ago the undeduplicated linker line contained hundreds duplicated library entries (it still does but not nearly as many).
The best possible solution would be if GNU ld started behaving the same way as VS linker and LLD. That way all major platforms would behave the same and things would get a lot simpler. In the mean time one should be able to simulate this with linker grouping flags:
- Go through all linker arguments and split them to libraries that use link_whole and those that don't. Throw away any existing linker grouping.
- Deduplicate and put the former at the beginning of the link line with the requisite link_full arguments.
- Deduplicate all entries in the list of libraries that don't get linked fully.
- Put the result of 3 on the command line in a single linker group.
This should work and would match fairly accurately what VS and LLD already do, so at least all cross platform projects should work out of the box already.
What about other platforms?
The code is here, feel free to try it out yourself.