当前位置:  开发笔记 > 编程语言 > 正文

在C中使用SDL2编译时遇到的问题

如何解决《在C中使用SDL2编译时遇到的问题》经验,为你挑选了1个好方法。

我一直在尝试编译一个简单的程序以测试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 

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

谢谢



1> keltar..:

SDL/SDL.h是SDL 1.2或更低版本。SDL2标头为SDL2/SDL.h

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

可能更便于移植的方式包括公正地SDL.h手动输入包含编译器的路径(-I如果是gcc,则带有标志),或者使用sdl2-config --cflags

推荐阅读
LEEstarmmmmm
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有