繁体   English   中英

在SDL 2程序运行时如何打印到控制台?

[英]How do I print to the console while an SDL 2 program is running?

我想在运行我的SDL 2程序时将一些调试内容打印到控制台,但似乎不可能。 printf("Hi!\\n")SDL_Log("Hi!\\n")都不会对我有任何帮助。

我甚至在初始化SDL之前尝试打印(以及在退出之后),但无济于事。 似乎只是导入 SDL库使得无法向控制台打印任何内容。

以下是我正在编译的参数,因为它可能与它有关:

g++ hello.cc -IC:\mingw_dev_lib\include\SDL2 -LC:\mingw_dev_lib\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -std=c++11

有任何想法吗?

所以,我弄清楚是什么阻止了我看到输出。 这些编译选项

-Wl,-subsystem,windows

实质上禁用控制台窗口,防止显示输出。 这对于游戏完成很有用,但对于调试很糟糕。 所以,我继续删除那些编译选项,现在printf()SDL_Log()工作得非常好。

我使用这种方法来调试控制台:

static ULONG_PTR GetParentProcessId() // By Napalm @ NetCore2K
{
    ULONG_PTR pbi[6];
    ULONG ulSize = 0;
    LONG (WINAPI *NtQueryInformationProcess)(HANDLE ProcessHandle, ULONG ProcessInformationClass,
            PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength); 
    *(FARPROC *)&NtQueryInformationProcess = 
        GetProcAddress(LoadLibraryA("NTDLL.DLL"), "NtQueryInformationProcess");
    if(NtQueryInformationProcess){
        if(NtQueryInformationProcess(GetCurrentProcess(), 0,
                    &pbi, sizeof(pbi), &ulSize) >= 0 && ulSize == sizeof(pbi))
            return pbi[5];
    }
    return (ULONG_PTR)-1;
}

static void _windows_init_console(int argc, char **argv) {
    (void)argc, (void)argv;
    ULONG_PTR ppid = GetParentProcessId();
    if(ppid == (ULONG_PTR)-1) {
        AllocConsole();
    } else {
        AttachConsole(ppid);
    }

    freopen("CONIN$", "r", stdin); 
    freopen("CONOUT$", "w", stdout); 
    freopen("CONOUT$", "w", stderr); 
}

GetParentProcessId来自Win32进程如何获取其父进程的pid? )。 工作得很好,但我仍然无法使用grep / findstr等工作(即'带有管道重定向的'true'标准输出)。

由于SDL2使用Windows子系统,因此可以使用Win32 API打开控制台窗口。

根据这篇博文

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    // SDL2 init code...

    // Just before the event loop
    AllocConsole();

    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
    int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
    FILE* hf_out = _fdopen(hCrt, "w");
    setvbuf(hf_out, NULL, _IONBF, 1);
    *stdout = *hf_out;

    HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
    hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
    FILE* hf_in = _fdopen(hCrt, "r");
    setvbuf(hf_in, NULL, _IONBF, 128);
    *stdin = *hf_in;

    // use the console just like a normal one - printf(), getchar(), ...
}

希望这可以帮助。

由于在使用mingw时窗口中的SDL2仍然存在问题,因此这是我找到并测试工作的更好的解决方案。

不要像其他人建议的那样删除-mwindows构建选项。 您应该添加`pkg-config --libs SDL2`作为构建选项,但是对于调试构建选项,您还应该在末尾添加-mconsole 它应该在-mwindows标志之后。

调试: `pkg-config --libs SDL2` -mconsole
发布: `pkg-config --libs SDL2`

注意 :我正在编译Windows 10,SDL2 v2.0.9,Msys64,mingw64,Code :: Blocks 17.12
`pkg-config --libs SDL2`扩展为:
-LC:/ msys64 / mingw64 / lib -lmingw32 -lSDL2main -lSDL2 -mwindows

参考文献:
SDL2:在pkg-config --libs输出#2419中保留-mwindows标志
configure:在MinGW下链接SDL时强制-mconsole

暂无
暂无

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

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