programming fun
TRANSCRIPT
Programming Fun
Author: Derek Dai
寫 code, 好玩
嗎 ?
Derek 說
寫 code, 當然不好玩
因為 ?
• 如果–只是想有個能頂著科技新貴的頭銜的工作–只是因為教育機構的廣告說資訊業很’高尚’而加
入–只是 8x5x52x30 機械化的動作著
• 這樣 , 何止是不好玩 , 是真他 X 的痛苦人生
那…怎麼辦 ?
• 丟棄 ‘寫 code’ 這回事• 修正觀念–軟體研發 , 從來就不是只有寫 code 一件事–彈性是要表現在架構上 , 而不是團隊的紀律 !
台灣在看軟體業時一直有這樣的誤解–設計 , 是要一直花心思維持的–創意 , 是解決問題的關鍵 ( 軟體從來就不是死
板板的 )–溝通 , 是團隊能成功的要素
還有
導入 Open Source
進入正題
• 將使用的一些玩意兒– Eclipse IDE– Cmake– git– GNU toolchain– GTK+• 完完全全的 free ( 自由 ) & free ( 免費 )
為什麼要用 open source?
• 跨平台 , 經驗重用• 習慣了 , 就會愛上它• 但…也有可能第一次接觸就恨死它– Package 散落各處–怎麼組合出 work 的東西 ?– Document, give me some document?– Where is pre-built binary for XXX platform? I don’t
wanna build it myself…
開發工具介紹• Eclipse + CDT: 好用的 C/C++ 跨平台 IDE– Netbeans 也不錯 , 但目前 CMake 還未支援
• CMake: 跨平台 /IDE/compiler 的 build/deploy tool
• git: 超強的分散式版本控制系統– TortoisGIT: Windows 下方便的 git frontend
• Mingw32: GNU toolchain 的 Windows port– binutils– GCC– GDB
開發工具間的關係
Eclipse
Mingw32
GDB
GCCGit
TortoiseGIT
GUI: 不一定要使用
輔助工具
核心開發工具CMake
GUI frontend
Source code version management
Generate IDE project file
Generate make file to build automatically
Debug
Build
以時間點來看開發工具的關係• 以 Madedit 寫了經典的“ hello world”, 以 gcc
build 成執行檔 , 以 gdb 除錯• Project 加進 2, 3 個檔案後 , build 變得複雜 ,
此時以 CMake 自動化 build 流程• Code 變大後 , 在 browse & debug 上相形困
難 , 以 CMake 產生 IDE 的 project file, 用 Eclipse 便利開發
• 多次變更 code 後 , 導致有不同時間的版本 , 可考慮以 git 管理
• 系統在功能性及穩定性達到要求後有發佈的需求 , CMake 可產生多樣的安裝包
建立開發環境 - 安裝 Mingw32
• http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/–執行 MinGW-5.1.6.exe 網路安裝界面後加選 g+
+ 及 make
–強烈建議安裝於 “ c:\MinGW”
–安裝後 , 請將 “ c:\MinGW\bin” 加到系統 PATH 環境變數中
建立開發環境 - 安裝 gdb
• http://sourceforge.net/projects/mingw/files/–選 ” GNU Source-Level Debugger” 並下載 6.6–先不要使用太新的版本 , 於 Eclipse 整合除錯
時會有問題 (6.8 似乎也是 OK 的版本 )–下載後 , 直接解壓至 “ c:\MinGW” 中
建立開發環境 - 安裝 CMake
• http://www.cmake.org/cmake/resources/software.html–下載並執行 cmake-2.8.0-win32-x86.exe 即可–無特殊安裝目錄需求–請將 CMake 安裝目錄下的 \bin 加進 PATH
建立開發環境 - 安裝 Eclipse + CDT
• Eclipse 以 Java 開發 , 需要 JRE– http://java.sun.com/javase/downloads/index.jsp–下載 JRE6 or JDK6–安裝
• Eclipse IDE for C/C++– http://www.eclipse.org/downloads/–選 Eclipse IDE for C/C++ Developers 的 Windows
版–下載後 , 解壓縮即可執行
建立開發環境 – 安裝 git
• http://code.google.com/p/msysgit/–下載最新版並安裝即可
• http://code.google.com/p/tortoisegit/– TortoiseGit 會使用到 msysygit• 這是隻包著糖衣的烏龜 – 在開發跨平臺的程式時 ,
不是一直都有整合得這麼好的 GUI 可用的–絕對要會下 git 的命令
建立開發環境 – 安裝 GTK+
• http://www.gtk.org/download-windows.html–下載 all-in-one bundle, 或是可以各別下載
runtime 及 developlement packages• 如果是後者 , 請先瞭解 libraries 間的相依性
–解壓後 , 將 bin 目錄加進 PATH 環境變數中 , 會用到下面的部份• glib 相關工具• pkg-config• Runtime libraries (.dlls)
建立 project HelloCmake
• 目的–確認 git, mingw32, gdb, Eclipse , CMake
• 建立目錄 HelloCMake, 並將接下來兩頁的 code 各存成 hellocmake.c 及 CMakeLists.txt–在跨平台的開發中 , 要特別注意檔案大小寫• Windows 中 file name 是 case insensitive, • Linux 則是 case sensitive• 所以 , 請假設所有平台都是 case sensitive• 但 , 請勿濫用 case sensitive (例如於同一目錄中放置同名不同大小寫的檔案 )
hellocmake.c
/* hellocmake.c */
#include <stdio.h>
int main()
{
printf("Hello Cmake\n");
return 0;
}
CMakeLists.txt
# CMakeLists.txt
PROJECT( HelloCMake )
SET( CMAKE_BUILD_TYPE Debug )
ADD_EXECUTABLE(
hellocmake #Executable name
hellocmake #Source file name
)
以 Cmake 產生 Makefile
• CMake 的其中一個功能是 Makefile generator–於 HelloCMake 目錄同一階建立
HelloCMake_build 目錄–於命令列 , cd 進入 HelloCMake_build, 並執行
• cmake -G"MinGW Makefiles" ..\HelloCMake• mingw32-make• hellocmake.exe
–執行程式後 , 應該可以看到 “ Hello Cmake” 的輸出
以 CMake 產生 Eclipse CDT project
• 進入 HelloCMake_build, 執行– cmake -G"Eclipse CDT4 - MinGW Makefiles" ..\HelloCMake
• 打開 Eclipse, 選 File -> Import… -> General -> Existing Projects into Workspace–於 Select root directory 中輸入
HelloCMake_build 所在完整路徑– Finish 後 , Project 即匯入
開始 debugging
• 雖然我們的程式太簡單沒 bug, 但還是可以用 gdb single step 玩玩– Double click “printf” 那行的左側 , 增加 break
point–於左側分頁展開 Binaries ->
right click “hellocmake.exe” ->Debug As -> Locale C/C++Application -> gdb Debugger
– Eclipse 即進入 Debug 模式
單步執行 HelloCMake
• 按 F5 可 step into, 追進即將執行的 function
• 按 F6 可 step over, 碰到 function 只當一個步驟走過
• 按 F7 可 step return, 離開目前的 function, 回到 caller 中的下個執行位置
• 按 F8 可 Resume, 讓程式自由的執行 , 直到碰到 break pointer, 或是被我們手動打斷
將 HelloCMake 加進版本控制
• 進到 HelloCMake 中• 執行
– git init
建立 git repository– git add *
將 hellocmake.c 及 CMakeLists.txt 進入版本控制的行列中
– git commit -m “Initial commit”
Commit 後 , source 才算真的進入 git, 版號加 1
結束
• 啥 ! 怎麼沒提到 GTK+ 勒 ? 是 download 心酸的喔 !–看官放心 ~耐心等等 , 下個例子就會用到