在 C 中使用 SDL2 进行编译的问题

Problems Compiling with SDL2 in C

我一直在尝试编译一个简单的程序来测试 SDL2,但出于某种原因,当我尝试这样做时,编译器说 SDL_Window 是未知类型。如果有人能告诉我我做错了什么,我将不胜感激。(我的操作系统是 Ubuntu linux)这是完整的输出:

thin.c: In function ‘main’:
thin.c:9:5: error: unknown type name ‘SDL_Window’
     SDL_Window *window;                    // Declare a pointer
     ^
thin.c:14:5: warning: implicit declaration of function ‘SDL_CreateWindow’ [-Wimplicit-function-declaration]
     window = SDL_CreateWindow(
     ^
thin.c:16:9: error: ‘SDL_WINDOWPOS_UNDEFINED’ undeclared (first use in this function)
         SDL_WINDOWPOS_UNDEFINED,           // initial x position
         ^
thin.c:16:9: note: each undeclared identifier is reported only once for each function it appears in
thin.c:20:9: error: ‘SDL_WINDOW_OPENGL’ undeclared (first use in this function)
         SDL_WINDOW_OPENGL                  // flags - see below
         ^
thin.c:35:5: warning: implicit declaration of function ‘SDL_DestroyWindow’ [-Wimplicit-function-declaration]
     SDL_DestroyWindow(window);

这里也是源代码。它来自 API 文档:

// Example program:
// Using SDL2 to create an application window

#include "SDL/SDL.h"
#include <stdio.h>

int main(int argc, char* argv[]) {

    SDL_Window *window;                    // Declare a pointer

    SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );

    // Check that the window was successfully created
    if (window == NULL) {
        // In the case that the window could not be made...
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }

    // The window is open: could enter program loop here (see SDL_PollEvent())

    SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

    // Close and destroy the window
    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}

最后是我发出的编译代码的命令:

gcc thin.c -o test -Wall -lSDL2

谢谢

似乎包含的路径不对。 确保使用 -I 开关并将路径设置为 SDL2 includes

SDL/SDL.h 是 SDL 1.2 或更早版本。 SDL2 header 是 SDL2/SDL.h.

这反映了错误消息 - SDL 1.2 没有 SDL_Window 类型或许多其他内容。

可能更便携的方法是仅包含 SDL.h 并手动将包含路径提供给编译器(使用 -I 标志,在 gcc 的情况下),或使用 sdl2-config --cflags.