diff --git a/libcxx/src/iostream.cpp b/libcxx/src/iostream.cpp --- a/libcxx/src/iostream.cpp +++ b/libcxx/src/iostream.cpp @@ -77,7 +77,7 @@ #endif ; -_LIBCPP_HIDDEN ios_base::Init __start_std_streams; +_LIBCPP_HIDDEN ios_base::Init __start_std_streams __attribute__((init_priority(101))); // On Windows the TLS storage for locales needs to be initialized before we create // the standard streams, otherwise it may not be alive during program termination diff --git a/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects/cout.init_priority.sh.cpp b/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects/cout.init_priority.sh.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects/cout.init_priority.sh.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// XFAIL: libcpp-has-no-stdout + +// Make sure that the iostreams are initialized before everything else. +// This has been an issue when statically linking libc++ in some contexts. +// See https://llvm.org/PR28954 for details. + +// RUN: %{build} +// RUN: %{run} > %t.out +// RUN: grep -e 'SHOULD BE PRINTED' %t.out + +#include +#include "test_macros.h" + + +struct Foo { + Foo() { + std::cout << "SHOULD BE PRINTED" << std::endl; + } +}; + +Foo foo; + +int main(int, char**) { + return 0; +}