简体   繁体   中英

SDL Hello World program does not output message

#include <iostream>
#include "SDL.h"
using namespace std;

int main() {
    cout << "hello world" << endl;
    return 0;
}

Error

17:23:46 **** Incremental Build of configuration Debug for project SDL2_program_59 ****
Info: Internal Builder is used for build
g++ "-ID:\\SDL2_inc_lib\\SDL2-2.0.8\\i686-w64-mingw32\\include\\SDL2" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\SDL2_program_59.o" "..\\src\\SDL2_program_59.cpp" 
g++ "-LD:\\SDL2_inc_lib\\SDL2-2.0.8\\i686-w64-mingw32\\lib" -o SDL2_program_59.exe "src\\SDL2_program_59.o" -lmingw32 -lSDL2main -lSDL2 
D:\SDL2_inc_lib\SDL2-2.0.8\i686-w64-mingw32\lib/libSDL2main.a(SDL_windows_main.o): In function `main_utf8':
/Users/slouken/release/SDL/SDL2-2.0.8-source/foo-x86/../src/main/windows/SDL_windows_main.c:126: undefined reference to `SDL_main'
/Users/slouken/release/SDL/SDL2-2.0.8-source/foo-x86/../src/main/windows/SDL_windows_main.c:126: undefined reference to `SDL_main'

I fixed the SDL_main error by adding argument to main() function such as:

#include <iostream>
#include "SDL.h"
using namespace std;

int main(int argv, char** args) {
    cout << "hello world" << endl;
    return 0;
}

I can compile and run the program without problem. But I cannot see the "hello world" message in the screen. If I comment out the SDL.h include line, I do see the message. What is the problem ?

Windows have big distinction between 'console' and 'GUI' applications. Recent SDL2 can handle both, but ultimately it is the linker that decides which application type you have. With gcc/mingw, it is controlled by -mconsole and -mwindows switches. Eg with your source code in question:

> g++ test.cpp -lmingw32 -lSDL2main -lSDL2 -mconsole
> a.exe
hello world
> g++ test.cpp -lmingw32 -lSDL2main -lSDL2 -mwindows
> a.exe
>

The output is still there, but stdout is no longer connected to console for GUI application so you don't see it in console, but you'll have it in debugger or if you'll redirect stdout to other place, eg:

> a.exe | more
hello world

It is also possible to handle console output on application side by redirecting stdin / stdout to CONIN$ and CONOUT$ , but that way I suppose you'll lose redirection ability on calling side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM