CMAKE_CXX_STANDARD_LIBRARIES_INIT or CMAKE_EXE_LINKER_FLAGS_INIT get repeated - causes link errors
Hello,
consider the following:
main.cpp:
#include <thread>
#include <cstdio>
int main()
{
std::thread t([] { puts("foo"); });
t.detach();
}
CMakeLists.txt:
project(foo)
add_executable(foo main.cpp)
toolchain.cmake:
string(APPEND CMAKE_CXX_FLAGS_INIT " -static")
# Needed because of this glibc / whatever bug: https://stackoverflow.com/a/31265512/1495627
# string(APPEND CMAKE_CXX_STANDARD_LIBRARIES_INIT " -Wl,--whole-archive -lpthread -Wl,--no-whole-archive")
# string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " -Wl,--whole-archive -lpthread -Wl,--no-whole-archive")
Invokation:
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=./toolchain.cmake
cmake --build build
If I uncomment either of the commented line in the toolchain, then I get a ton of "duplicated definition" errors. This is because the link line looks like this (line breaks added manually):
/usr/bin/c++ -static -static -rdynamic CMakeFiles/foo.dir/main.cpp.o -o foo \
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive \
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive
As you can see, the flags are repeated twice which in this case does not work at all as it will try to embed libpthread.a twice.
How can that be fixed, at the toolchain level ?