C不会包含sdl.h编译

时间:2021-09-13 09:44:41

I am trying to get SDL library working on my macbook to write a small game(probably tetris) in c. I read and followed the installation instructions for SDL at https://wiki.libsdl.org/Installation.

我想让SDL库在我的macbook上工作,在c中写一个小游戏(可能是俄罗斯方块)。我在https://wiki.libsdl.org/Installation上阅读并遵循了SDL的安装说明。

However, when I tried to compile the following c code:

但是,当我尝试编译以下c代码时:

#include <SDL2/SDL.h>
#include <stdio.h>
int main()
{
    SDL_Init(SDL_INIT_VIDEO);
    printf("Window Initialization!\n");
    SDL_Window *window;
    //printf("SDL_WINDOWPOS_CENTERED is %d\n", SDL_WINDOWPOS_CENTERED);
    window = SDL_CreateWindow(
                             "SDL2 Test",   
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              640, 480, 0);
    if(window == NULL){
        printf("Creation of Window Failed\n");
        SDL_Quit();
        return -1;
    }
    SDL_Delay(3000);    //delay for 3000 ms
    SDL_Renderer* rend = NULL;
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

with the command:

使用命令:

gcc main.c -o run

The following error is printed:

打印以下错误:

Undefined symbols for architecture x86_64:
"_SDL_CreateWindow", referenced from:
  _main in sdl1-a80c27.o
"_SDL_Delay", referenced from:
  _main in sdl1-a80c27.o
"_SDL_DestroyWindow", referenced from:
  _main in sdl1-a80c27.o
"_SDL_Init", referenced from:
  _main in sdl1-a80c27.o
"_SDL_Quit", referenced from:
  _main in sdl1-a80c27.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Since I did make and sudo make install, according to the website, the SDL should be "universal" now. So I assume I would not need linker. But apparently that is not the case.

自从我做了make和sudo make install之后,根据网站的说法,SDL现在应该是“通用的”。所以我假设我不需要链接器。但显然事实并非如此。

Previously I also tried to just add framework file into /Library/Frameworks. And then if I did:

以前我还尝试将框架文件添加到/ Library / Frameworks中。然后,如果我这样做:

gcc sdl1.c -o run -F/Library/Frameworks/SDL2.framework/Headers -framework SDL2

The code can get compiled. However, sometimes I want to include SDL2_image.h as well and adding another -F/.../ won't work.

代码可以编译。但是,有时我想要包含SDL2_image.h并添加另一个-F /.../将不起作用。

I would really appreciate any suggestions/advice. Thanks!

我真的很感激任何建议/意见。谢谢!

1 个解决方案

#1


5  

From the documentation

从文档中

gcc sdl1.c -o run `sdl2-config --cflags --libs`

sdl comes with a tool to generate for you all the flags, just use it, it is the best way to maintain your build system.

sdl附带了一个为您生成所有标志的工具,只需使用它,它是维护构建系统的最佳方式。

#1


5  

From the documentation

从文档中

gcc sdl1.c -o run `sdl2-config --cflags --libs`

sdl comes with a tool to generate for you all the flags, just use it, it is the best way to maintain your build system.

sdl附带了一个为您生成所有标志的工具,只需使用它,它是维护构建系统的最佳方式。