【发布时间】: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