简体   繁体   English

当我尝试使用 cout 打印语句时,SDL 未返回 output

[英]SDL returns no output when I try to print statements using cout

I installed the MinGW version of SDL from their website .我从他们的网站安装了 MinGW 版本的 SDL。

I created a sample piece of code just to test if I could include the library without any errors.我创建了一段示例代码只是为了测试我是否可以在没有任何错误的情况下包含该库。

#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;
}

I also created a Makefile:我还创建了一个 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)

If I don't include the int argc, char* argv[] inside of int main() and try to ming32-make , it throws an error:如果我不在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

When I include int argc, char* argv[] , it doesn't give any errors but doesn't print anything either.当我包含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>

When I use make instead of mingw32-make , the output remains the same.当我使用make而不是mingw32-make时,output 保持不变。

I am using VSCode and I have included the header files and lib files in an src folder in the same directory as my script and also moved the SDL2.dll file in the root folder:我正在使用 VSCode,我已将 header 文件和 lib 文件包含在与我的脚本相同的目录中的 src 文件夹中,并将 SDL2.dll 文件移动到根文件夹中:

代码文件

My C++ Configuration on VSCode:我的 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

I had also recieved SDL.h: file or directory not found errors before this and I fixed them by creating the Makefile.我还收到了 SDL.h: file or directory not found错误,我通过创建 Makefile 修复了它们。

Is there something I'm missing?有什么我想念的吗? Does SDL not output to stdout, because I've seen tutorials online and they are able to get outputs from cout fine on them. SDL 不是 output 到 stdout,因为我在网上看过教程,他们能够从cout获得输出。

I am expecting cout to work when I run the script.当我运行脚本时,我希望 cout 能够工作。

-Wl,-subsystem,windows (aka -mwindows ) hides the console window, and with it all output. Remove it, and use it only in the release builds. -Wl,-subsystem,windows (又名-mwindows )隐藏控制台 window,以及所有 output。删除它,并仅在发布版本中使用它。

-w suppresses all warnings -w禁止所有警告

This is extremely unwise.这是极不明智的。 Prefer -Wall -Wextra -Wdeprecated to enable most common warnings, plus -std=c++20 -pedantic-errors to enforce standard compliance (replace 20 with the latest version supported by your compiler).首选-Wall -Wextra -Wdeprecated以启用最常见的警告,加上-std=c++20 -pedantic-errors以强制符合标准(将20替换为编译器支持的最新版本)。


As suggested by @keltar, you might be able to get output even from a program built with -mwindows if you redirect it to a file, using my_program.exe >output.txt .正如@keltar 所建议的,如果您使用my_program.exe >output.txt将它重定向到一个文件,您甚至可以从使用-mwindows构建的程序中获得 output 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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