[英]SDL returns no output when I try to print statements using cout
我從他們的網站安裝了 MinGW 版本的 SDL。
我創建了一段示例代碼只是為了測試我是否可以在沒有任何錯誤的情況下包含該庫。
#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL INIT FAILED" << endl;
return 1;
}
cout << "SDL INIT SUCCEEDED" << endl;
SDL_Quit();
return 0;
}
我還創建了一個 Makefile:
#OBJS specifies which files to compile as part of the project
OBJS = main.cpp
#CC specifies which compiler we're using
CC = g++
#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Isrc\includes
#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -Lsrc\lib
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS = -w -Wl,-subsystem,windows
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = main
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
如果我不在int main()
中包含int argc, char* argv[]
並嘗試ming32-make ,它會拋出錯誤:
C:\Users\username\Documents\Projects\C++\SDL_test> mingw32-make
g++ main.cpp -Isrc\includes -Lsrc\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o main
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: src\lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.26.2-source/foo-x64/../src/main/windows/SDL_windows_main.c:82: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [Makefile:26: all] Error 1
當我包含int argc, char* argv[]
時,它不會給出任何錯誤,但也不會打印任何內容。
C:\Users\username\Documents\Projects\C++\SDL_test> mingw32-make
g++ main.cpp -Isrc\includes -Lsrc\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o main
C:\Users\username\Documents\Projects\C++\SDL_test>
當我使用make
而不是mingw32-make
時,output 保持不變。
我正在使用 VSCode,我已將 header 文件和 lib 文件包含在與我的腳本相同的目錄中的 src 文件夾中,並將 SDL2.dll 文件移動到根文件夾中:
我的 C++ 在 VSCode 上的配置:
Compiler Path: C:\MinGW\bin\g++.exe
Compiler Arguments:
IntelliSense mode: gcc-x64 (legacy) // Because using anything else says the the mode is incompatible with the compiler path.
Include path:
${workspaceFolder}/**
${workspaceFolder}/src/includes
我還收到了 SDL.h: file or directory not found錯誤,我通過創建 Makefile 修復了它們。
有什么我想念的嗎? SDL 不是 output 到 stdout,因為我在網上看過教程,他們能夠從cout
獲得輸出。
當我運行腳本時,我希望 cout 能夠工作。
-Wl,-subsystem,windows
(又名-mwindows
)隱藏控制台 window,以及所有 output。刪除它,並僅在發布版本中使用它。
-w
禁止所有警告
這是極不明智的。 首選-Wall -Wextra -Wdeprecated
以啟用最常見的警告,加上-std=c++20 -pedantic-errors
以強制符合標准(將20
替換為編譯器支持的最新版本)。
正如@keltar 所建議的,如果您使用my_program.exe >output.txt
將它重定向到一個文件,您甚至可以從使用-mwindows
構建的程序中獲得 output 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.