我在尝试编译 C 程序时遇到问题。我在 macOS Catalina 上并使用 clang 进行编译。
当我尝试SDL_Init( SDL_INIT_VIDEO );编译 clang 时告诉我链接器命令失败
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in main-1defaf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我使用brew来安装SDL2和brew install SDL2. 因此,我尝试将这些标志传递给链接器,但没有一个起作用。
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-L/usr/local/Cellar/sdl2/2.0.12_1/lib
Run Code Online (Sandbox Code Playgroud)
由于它们不起作用,我然后尝试添加该-framework标志。我按照此处找到的说明安装了框架。然而该网站没有提到如何与 clang 一起使用它。Clangs 文档没有提到我是否需要指向框架文件或目录,因此我尝试了几种不同的方法,但无法正常工作。
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-framework SDL2
ld: framework not found SDL2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-framework /Library/Framework/SDL2.framework
ld: framework not found /Library/Framework/SDL2.framework
clang: error: linker command failed with exit code 1 (use -v to see invocation)
> clang main.c -L/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2 \
-framework /Library/Frameworks/SDL2.framework/SDL2
ld: framework not found /Library/Frameworks/SDL2.framework/SDL2
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我也尝试过仅使用 XCode 进行编译,它构建成功,但无法运行并出现以下错误。我按照上面链接的教程中的说明进行操作。
2020-06-30 16:07:36.220432-0700 sdl_test[1867:31873] Metal API Validation Enabled
2020-06-30 16:07:36.273590-0700 sdl_test[1867:32300] flock failed to lock maps file: errno = 35
2020-06-30 16:07:36.274022-0700 sdl_test[1867:32300] flock failed to lock maps file: errno = 35
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
我已经重新启动了计算机并清理了构建,并且根据一些谷歌搜索,这似乎是在最新版本的 XCode 中引入的,需要解决。
我想坚持使用 clang,并且不确定我还能尝试什么来编译它。我想获得有关后续步骤的一些指导。
这是我试图运行的代码。
#include <SDL2/SDL.h>
#include <stdio.h>
int main(int argc, const char * argv[]) {
SDL_Init( SDL_INIT_VIDEO );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您缺少-lSDL2引入共享库,如果您要使用,/usr/local/Cellar/sdl2/2.0.12_1/include/SDL2则应该只使用#include <SDL.h>.
考虑使用pkg-config来处理包含和链接标志。
clang main.c -o main `pkg-config --libs --cflags sdl2`
Run Code Online (Sandbox Code Playgroud)