Running SDL2 on MacOS
Table of Contents
1. intro
嘗試在 MacOs 上 玩下SDL2,但是遇到坑了,簡單紀錄下。
Try to install SDL2 on Mac, but have some issues. As a record:
machine: macbook pro - M2 OS: Sonoma 14.0 Homebrew version: 4.2.0 Date issue: 2023-12-23
2. Issue
用 Homebrew 安裝 SDL2,然後在 C include SDL2/SDL.h, 之後進行編譯系統會顯示 fatal error: ‘SDL2.h‘ file not found。
透過 homebrew 直接安裝 SDL2:
brew search sdl2 ==> Formulae sdl2 ✔ sdl2_gfx ✔ sdl2_image ✔ sdl2_mixer ✔ sdl2_net ✔ sdl2_sound ✔ sdl2_ttf ✔
起一個簡單的 C program:
#include <stdio.h> #include <SDL2/SDL.h> int main() { printf("Game is running...\n"); return 0; }
編譯:
gcc -Wall -std=c99 ./src/*.c -o game
Error:
./src/main.c:2:10: fatal error: 'SDL2.h' file not found #include <SDL2.h> ^~~~~~~~ 1 error generated.
3. Solution
最後我辦法是手動把 homebrew 的 SDL2 拉到項目裡來
Homebrew 安裝 SDL2 的位置:
/opt/homebrew/Cellar/sdl2
複製 SDL2 include 到自己的項目:
cp -R /opt/homebrew/Cellar/sdl2/2.28.5/include ~/project/
複製 SDL2 lib 到自己的項目:
cp -R /opt/homebrew/Cellar/sdl2/2.28.5/lib ~/project/
最後修改 build command, 加載 includ folder 和 lib folder :
gcc -Wall -std=c99 -I include -L lib -lSDL2-2.0.0 ./src/*.c -o game
編譯成功!