Try   HackMD

在OSX Catalina 上安裝 Allegro 5

因為在升級上osx caralina 以後,所有32-bit的程式都不能夠再被系統執行,但是在各位寫final project 的時候新的Allegro還沒辦法被套件管理器正確支援。

因此我們今天就要來教大家如何在自己的電腦上 build Allegro 5.2.6。

環境準備

首先你需要安裝Homebrew

Homebrew 套件管理器介紹 https://brew.sh/index_zh-tw

一、收先我們打開 Mac 的終端機輸入:

$ /usr/bin/ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

他就會開始自己安裝了。

安裝完以後你可以接著輸入:

$ brew --version

就會像下面一樣看到,brew 的版本

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

一些基本得操作

  1. brew install packahe_name 安裝套件
  2. brew search 搜尋需要的套件

正式開始安裝

相依套件

收先在開始自己 build 以前我們要先安裝 Allegro 5 的相依套件,那我們一樣用brew 來安裝

$ brew install dumb flac freetype libogg  libvorbis opusfile physfs theora webp git cmake gcc

安裝完成以後我們就可以去Allegro 的 github page 下載檔案了~ https://github.com/liballeg/allegro5

我們打開終端機輸入:

$ git clone https://github.com/liballeg/allegro5.git && cd allegro5

你會發現我們已經在 allegro5 這個資料夾內了

接下來我們在這邊新增一個編譯用的資料夾,通常來說我們會做這樣一個資料夾來讓編譯的檔案都放在一起,這樣可以有效的跟原本的source code 隔離,如果我們想要清理環境,就只要刪除build 這個資料夾就好了

$ mkdir build
$ cd build

編譯

接下來進入編譯過程,首先要做得事情就是 configure Allegro

$ cmake ..

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

configure 完成後就可以開始編譯拉,這裏會用到 make 指令至於這個指令工作的機制稍後會提到

$ make

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

build 完以後我們要來安裝他。

$ sudo make install

這時候需要輸入你得密碼

注意! 密碼不會出現在螢幕上

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

他會把需要得檔案裝在 /usr/local/lib 裡面,header檔則是在 /usr/local/include/底下。

在 Mac 上使用 Allegro 5

原本在 Xcode 上面按下一個按鈕就可以編譯這種美好的事情現在不可能拉。
但是還是有好用的IDE可以用得~

那就是 VScode 拉

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

至於要怎麼裝就請到官網去看 https://code.visualstudio.com/

然後我們需要安裝C/C++的插件
按下左邊這個按鈕

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

搜尋 C/C++,點擊安裝

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

介面看起來大概像這個子~

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接下來我們要來講解編譯的事情,你能夠用 Allegro 這個 libiarrays 的內容來寫扣是且執行是因為你在編譯的時候,編譯器可以找到相對印的函數來把你的程式轉乘成電腦看得懂的語言,這是在 linker 時期完成的(有興趣得同學可以自己來這邊了解編譯器的運作過程 https://medium.com/@aesl/understanding-compilers-for-humans-ba970e045877)。
其實不只是 Allegro ,還記得每次寫扣前都要載入的標頭檔嗎? 你可以使用printf這些函數的原因也是因為如此。
C Complier 之所以可以幫你翻譯printf是因為他認得 printf這個東西是在 stdio.h 這個函式庫裡被定義的。
但是Complier 並不認得 allegro/allegro.h 這個函式庫,那這時候我們就要手動告訴他,因此我們需要一個工具。

pkg-config

pkg-config 是一個在原始碼編譯時查詢已安裝的庫的使用介面的電腦工具軟體。pkg-config原本是設計用於Linux的,但現在在各個版本的BSD、windows、Mac OS X和Solaris上都有著可用的版本。

它輸出已安裝的庫的相關資訊,包括:

  • C/C++編譯器需要的輸入參數
  • 連結器需要的輸入參數
  • 已安裝軟體包的版本資訊

一如往常的,我們先來安裝他:

$ brew install pkg-config

當你輸入下面這個指令pkg-config --libs allegro-5的時候你就會發現,那吐回給你一個路徑,這就是 Allegro 的 object file 和 header file 擺放的地方。

$ pkg-config --libs allegro-5

  -L/usr/local/lib -lallegro

那接下來請各位同學用 vscode 打開助教給的 template 利用終端機進入,task_5 這個資料夾,我們來實際編譯看看 task_5_ans.c這個檔案。

$ gcc task_5_ans.c -o task_5.out $(pkg-config allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5 allegro_dialog-5 allegro_primitives-5 allegro_audio-5 allegro_acodec-5 --libs --cflags) -lallegro -lallegro_main

解釋一下

  • gcc 是調用 complier 的指令。
  • 接著的是當案名稱。
  • -o 後面的是編譯出來的執行檔名稱,不加的話會是 a.out。
  • 後面的一長串是編譯的flag 這邊是調用pkg-config 來把allegro 的路徑告訴編譯器讓他認得這些函數。

那接下來我們就可以執行他拉~

$ ./task_5.out

如果成功的話應該會看到,類似像這樣的畫面

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

到此為止你就成功了~

還沒完,你不覺得每次編譯都要打這些東西很麻煩?有沒有更好的方法來簡化編譯得過程啊~~

有的有的,請繼續往下看

Makefile

網路上有不少教學例如 https://ccckmit.gitbooks.io/lowlevelc/content/makefile.html

我相信這對同學來說有點難,不過沒關係能夠理解就理解,如果不行就照抄我給的範例。
但是還是希望你把下面得內容看完,因為這樣你才知道要改什麼內容。

以下是這次 Final project 給的 makefile

ALLEGRO_LIBRARIES := allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5 allegro_dialog-5 allegro_primitives-5 allegro_audio-5 allegro_acodec-5 ALLEGRO_FLAGS := $(shell pkg-config --cflags --libs $(ALLEGRO_LIBRARIES)) -lallegro -lallegro_main CC := gcc OUT:= task_5.out MAIN:= task_5_ans.o all: $(MAIN) $(CC) -o $(OUT) $(MAIN) $(ALLEGRO_FLAGS) clean: rm $(OUT)

可以觀察到,我們把 ALLEGRO_LIBRARIES , ALLEGRO_FLAGS 就是拿來定義編譯得參數

比較需要注意的是,MAIN 和 OUT 這個參數。他們分邊定義了.c 檔的名稱以及輸出檔案的名稱。就根據你現在的檔案名稱來更改,其餘的東西都不用動。

要用的時候只要打

$ make

就可以了
那如果想要刪掉最後輸出的執行檔的話,只要打

$ make clean

就好了~~~

希望各位同學可以順利的寫完 final project

程設二的部分

Makefile

CC := g++ CFLAGS := -Wall -std=c++17 -O2 -v ALLEGRO_LIBRARIES := allegro-5 allegro_image-5 allegro_font-5 allegro_ttf-5 allegro_dialog-5 allegro_primitives-5 allegro_audio-5 allegro_acodec-5 ALLEGRO_FLAGS := $(shell pkg-config --cflags --libs $(ALLEGRO_LIBRARIES)) -lallegro -lallegro_main OUT:= game SOURCE = Main.cpp Attack.cpp Circle.cpp GameWindow.cpp global.cpp Level.cpp Menu.cpp Monster.cpp Slider.cpp Tower.cpp OBJ = Main.o Attack.o Circle.o GameWindow.o global.o Level.o Menu.o Monster.o Slider.o Tower.o all: $(CC) -c -g $(CFLAGS) $(SOURCE) $(ALLEGRO_FLAGS) $(CC) $(CFLAGS) -o $(OUT) $(OBJ) $(ALLEGRO_FLAGS) rm $(OBJ) .PHONY:clean clean: rm $(OUT)