SDL Hello World程序不输出消息

时间:2018-05-18 09:39:09

标签: c++ dll sdl

#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包含行,我确实看到了消息。问题是什么 ?

1 个答案:

答案 0 :(得分:0)

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应用程序的控制台,因此您不会在控制台中看到它,但是您将在调试器中使用它,或者您将重定向{{ 1}}到其他地方,例如:

stdout

通过将> a.exe | more hello world / stdin重定向到stdoutCONIN$,也可以在应用程序端处理控制台输出,但这样我认为你将失去重定向呼唤方的能力。

相关问题