Search code examples
c++cmdprintfsdl-2

Unable to print to console in C++


I've been having some trouble with printf, stdout & stderr.

I've seen the questions like this, with the issue being related to \n on here already, but I'm already ending my strings with \n to no avail.

I'm fairly certain that this is due to compiler settings, because printing on the default settings works fine. However, I can't use the default settings because I need to use a library (SDL2)

Here's my compile script:

g++ main.cpp -o main.exe -IC:/cppLibs/SDL2-2.0.14/x86_64-w64-mingw32/include/SDL2/ -LC:/cppLibs/SDL2-2.0.14/x86_64-w64-mingw32/lib/ -lmingw32 -lSDL2main -lSDL2 -mwindows -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -static-libgcc

And here's the code that's not printing:

#include <SDL.h>
#include <stdio.h>
#include <iostream>

int main (int argc, char* args[])
{
    setvbuf(stdout, NULL, _IONBF, 0);

    fprintf(stdout, "test\n");
    fflush(stdout);
}

I've tried various variations of this, and cout doesn't do anything either

I'm on Windows 10, my compiler is mingw64 10.3.0 (g++), and I'm using Visual Studio Code as my IDE.

I'm new to StackOverflow, so if this is the wrong place to ask, or if I'm asking incorrectly, then I apologise


Solution

  • My issue was that I'd copied some compiler arguments off of google. One of them, "-mwindows" suppresses console output.

    @HolyBlackCat found this, but left a comment rather than an answer, so I can't mark it as a solution. If Holy reposts their comment as an answer, I'll delete this, and give them the credit