gcc linker library ordering issue with diamond INTERFACE dependencies
Injecting Interface targets into a build can confuse CMake about dependency graphs. Imported or system libraries appearing in the dependency chain are filtered to a single instance, but that instance is added to the list of libraries to be linked the first time it appears, not the last. This causes link errors on some compilers.
I've attached a simple CMakeList.txt that demonstrates the problem.CMakeLists.txt Note that this is not actually what we're doing; it's the simplest example I could come up with to demonstrate the issue.
Assume src/main.c, src/a.c, src/b.c, src/header.h exist and compile correctly.
Applying the attached CMakeLists.txt results in the following link ordering:
CMakeFiles/LibOrderImportTest.dir/src/main.c.o -o LibOrderImportTest liblib_a.a -lm liblib_b.a
In contrast with the desired link ordering:
CMakeFiles/LibOrderImportTest.dir/src/main.c.o -o LibOrderImportTest liblib_a.a liblib_b.a -lm
Note that the system library '-lm' appears once only (intended), but not at the end of the list (not intended).