簡體   English   中英

SDL Hello World程序不輸出消息

[英]SDL Hello World program does not output message

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

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

錯誤

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'

我通過向main()函數添加參數來修復SDL_main錯誤,例如:

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

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

我可以毫無問題地編譯和運行程序。 但是我在屏幕上看不到“ hello world”消息。 如果我注釋掉SDL.h包含行,則會看到該消息。 問題是什么 ?

Windows在“控制台”和“ GUI”應用程序之間有很大的區別。 最新的SDL2可以處理這兩種情況,但最終由鏈接器決定您擁有哪種應用程序類型。 對於gcc / mingw,它由-mconsole-mwindows開關控制。 例如,有問題的源代碼:

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

輸出仍然存在,但是stdout不再連接到GUI應用程序的控制台,因此您不會在控制台中看到它,但是可以在調試器中找到它,或者將stdout重定向到其他地方,例如:

> a.exe | more
hello world

通過將stdin / stdout重定向到CONIN$CONOUT$ ,也可以在應用程序端處理控制台輸出,但是那樣我想您將在調用端失去重定向功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM