C不会使用sdl.h进行编译

时间:2017-05-12 05:14:40

标签: c gcc compiler-errors sdl sdl-2

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

但是,当我尝试编译以下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;
}

使用命令:

gcc main.c -o run

打印以下错误:

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)

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

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

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

代码可以编译。但是,有时候我想要包含SDL2_image.h并添加另一个-F /.../赢了。

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

1 个答案:

答案 0 :(得分:5)

来自文档

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

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