We are having a strange problem when linking a shared library (Foo) with another internal static library (Bar). Bot libraries are built on the same machine within the same CMake build directory.
It is clear that code in shared libraries must be position independent, so we set PIC on any static library dependencies like so:
set_property(TARGET Bar PROPERTY POSITION_INDEPENDENT_CODE True)
And we link the library in the regular way, using target_link_libraries(Foo PUBLIC Bar)
.
This works fine with GCC 4.8, ld 2.22 and CMake 3.10.1.
However, for some reason, with GCC 7, ld 2.29.1 and CMake 3.10.2 linking fails with multiple undefined reference
errors for different functions in Bar. The functions are defined and implemented in Bar, used Foo and the whole thing compiles with GCC4.8. So what gives?
We are also aware of the dual ABI situation and potential issues with std::string
. (See https://stackoverflow.com/a/49119902/2095190) However the error does not fit the pattern.
I have some thoughts, though none of them explain why it works with the older toolchain:
- Possibly the order of the linker options is an issue?
- Missing a transitive dependency? (see answer: Static libraries linked against other static libraries with CMake - one works, one doesn't. Why?)
Our project is relatively complex, I will try to isolate the error next week.