【问题标题】:C won't compile with sdl.h includedC 不会在包含 sdl.h 的情况下编译
【发布时间】:2017-05-12 05:14:40
【问题描述】:

我正在尝试让 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 现在应该是“通用的”。所以我认为我不需要链接器。但显然情况并非如此。

以前我也尝试将框架文件添加到 /Library/Frameworks.然后如果我这样做了:

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

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

我非常感谢任何建议/建议。谢谢!

【问题讨论】:

  • 你可能需要用-lSDL编译。
  • 获取标志只需使用sdl2-config --cflags --libs

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


【解决方案1】:

来自文档

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

sdl 自带一个工具来为你生成所有的标志,使用它,它是维护你的构建系统的最佳方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2018-04-26
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多