The following code (compiled as part of a big project) does not print numbers (not just one-byte integers, but any of them: std::int32_t
, std::size_t
, double
, etc). Of course, this code compiled on its own works perfectly, and therefore it is impossible to provide a minimal example which reproduces this problem: something in this big project causes it to happen, but I can't put my finger on what exactly causes it - hence this question.
The code:
std::int32_t n = 42;
std::cout << "test 1" << std::endl; // prints
std::cout << 3.14 << std::endl; // doesn't print
std::cout << 456464 << std::endl; // doesn't print
std::cout << n << std::endl; // doesn't print
std::cout << "test 2" << std::endl; // still doesn't print
printf("printf: %d\n", n); // prints
std::cout.clear();
std::cout << "test 3" << std::endl; // prints
std::cout << 42 << std::endl; // doesn't print
Output:
test 1
printf: 1
test 3
So, number literals 3.14
and 456464
and variable n
are not printed as well as string test 2
(I attempt to print a string after failed printing of an integer). The printf
function works. I use std::endl
so it should not be any kind of buffering problem.
Using std::cout.clear()
seems to fix the problem (we can print strings again), but only until another number is printed.
What kind of manipulation with output streams may potentially cause this? Yes there is using namespace std;
.
The code is compiled with -std=c++11
using GCC toolchains (I tried various versions including 5.3.1, 6.3.1, and 8.2.0 - the result is always the same).
Compilation command line:
g++ c -MMD -pipe -std=c++11 -fPIC -O3 \\
-fmax-errors=3 -msse4.1 -mavx2 source.cpp -o target/objects/source.o
Linking command line:
g++ <object files> -o executable -s \\
-Wl,--build-id=uuid -static-libstdc++ -pthread -Wl,--no-undefined