gpt4 book ai didi

c - 在 C 中使用 SDL2 进行编译时出现问题

转载 作者:行者123 更新时间:2023-12-02 02:13:35 25 4
gpt4 key购买 nike

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

谢谢

最佳答案

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

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

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

关于c - 在 C 中使用 SDL2 进行编译时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com