c0n*_*ate 2 c++ sdl mingw sdl-2
我从他们的网站安装了 SDL 的 MinGW 版本。
我创建了一段示例代码,只是为了测试是否可以包含该库而不会出现任何错误。
#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;
}
Run Code Online (Sandbox Code Playgroud)
我还创建了一个 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)
Run Code Online (Sandbox Code Playgroud)
如果我不包含int argc, char* argv[]内部int main()并尝试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
Run Code Online (Sandbox Code Playgroud)
当我包含时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>
Run Code Online (Sandbox Code Playgroud)
当我使用make代替 时mingw32-make,输出保持不变。
我正在使用 VSCode,并且已将头文件和 lib 文件包含在与脚本相同的目录中的 src 文件夹中,并将 SDL2.dll 文件移至根文件夹中:
我在 VSCode 上的 C++ 配置:
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
Run Code Online (Sandbox Code Playgroud)
在此之前我还收到了SDL.h: file or directory not find错误,我通过创建 Makefile 修复了这些错误。
我有什么遗漏的吗?SDL 是否不输出到标准输出,因为我在网上看过教程,并且他们能够从coutFine 中获取输出。
我希望在运行脚本时 cout 能够工作。
-Wl,-subsystem,windows(又名-mwindows)隐藏控制台窗口以及所有输出。删除它,并仅在发布版本中使用它。
-w抑制所有警告
这是极其不明智的。首选-Wall -Wextra -Wdeprecated启用最常见的警告,并-std=c++20 -pedantic-errors强制执行标准合规性(替换20为编译器支持的最新版本)。
-mwindows正如 @keltar 所建议的,如果您使用 .redirect 将其重定向到文件,您甚至可以从使用 构建的程序中获取输出my_program.exe >output.txt。