簡體   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