Download - Extern 修飾詞

Transcript
Page 1: Extern 修飾詞

EXTERN修飾詞extern

Page 2: Extern 修飾詞

extern 變數在全域中不能重複宣告相同的變數名稱,但不同程式碼可能會用到相同的變數。 extern 修飾詞代表這個變數宣告在其他地方。 函數也可加上 extern ,但是可加可不加

( 函數模型 ) EX : void func(); extern void func();

Page 3: Extern 修飾詞

extern example //XD.c int XD=100; ------------------------------------------------------ //main.c #include<stdio.h> extern int XD; int main(){

printf(“%d\n”,XD);}

Page 4: Extern 修飾詞

extern 的初始化 若在宣告具有 extern 修飾的變數時初始化,會視為沒有 extern ,需要特別注意。 extern int XD=2; Int XD=2;

Page 5: Extern 修飾詞

STATIC修飾詞static

Page 6: Extern 修飾詞

static 變數 經由 static 修飾過的變數,經過宣告後,記憶體就不會改變,下次執行時不再重新宣告。 void GO(){

static int c=0; printf(“%d\n”,++c);}

int main(){ int i; for(i=0;i<10;i++) GO();}

Page 7: Extern 修飾詞

static 的另一個意義 加上 static 修飾詞的變數 ( 或函數 ) ,只會被允許在這個檔案中使用,其他程式碼都無法使用。

Page 8: Extern 修飾詞

GCCgcc compiler

Page 9: Extern 修飾詞

gcc gcc 是 C 的編譯器,在 UNIX 環境下可以直接使用,在 windows 環境下可以下載或從 Dev-C++ 資料夾中的 bin 資料夾中找到。 在 windows 環境下若設環境變數,則可在 command line 直接呼叫。

Page 10: Extern 修飾詞

使用 gcc 編譯檔案 $gcc -o 輸出檔名 要編譯的檔案 (s)

若無指定輸出檔名, UNIX 下預設為a.out , windows 下預設為 a.exe 。

Page 11: Extern 修飾詞

編譯的過程 編譯檔案

( 將原始碼編譯 ) 產生目的檔

( 產生 .o 檔 ) 鏈結函式庫

( 根據目的檔及使用者的指令鏈結需要用到的函式庫 ) 產生執行檔

( 產生最後的執行檔 )

Page 12: Extern 修飾詞

使用 gcc 產生目的檔 $ gcc –c 原始碼 .c 則會產生原始碼 .o 的目的檔 若要將目的檔產生執行檔,與一般的編譯方式相同。

Page 13: Extern 修飾詞

編譯優化參數 -O0 :預設狀態 -O1 :初步最佳化 -O2 :進一步最佳化,為推薦的優化。 -O3 :不要用,很可怕。最危險的編譯等級,有可能發生錯誤,在

gcc4.x 中甚至不被推薦使用。 -pipe :加速編譯時間,對結果無影響。

Page 14: Extern 修飾詞

MAKE FILE

Page 15: Extern 修飾詞

make make 是一個很方便的工具,可以依據提供的描述檔,迅速幫你執行完很多編譯相關的指令。 與 gcc 相同, UNIX 環境下有預設, windows 環境下則需要下載並設定環境變數。

Page 16: Extern 修飾詞

最簡單的編譯 $make code $gcc code.c -o code

這是 make 沒有其他參數的情況 需要特別注意這邊的檔名不加附檔名 若附檔名為 c 以 gcc 編譯,若為 cpp 則以 g++ 編譯。

Page 17: Extern 修飾詞

描述文件 make 的描述文件代表的是程式碼與程式碼之間的依賴關係 ( 規則 ) ,並依照此關係依序編譯。 目標檔案 : 需要檔案 1 需要檔案 2… <TAB> 產生指令 all:test.o test2.o

gcc test.o test2.o -o test.exe test.o:test.c

gcc -c test.c Test2.o:test2.c

gcc -c test2.c

Page 18: Extern 修飾詞

make 的執行過程 make 會從名稱為 all( 不一定產生的檔名就叫做 all) 的規則執行 ( 若沒有 all 則從第一個執行 )

接著尋找目錄中是否存在所有需要檔案,若有缺少則尋找其他規則是否可以產生需要檔案。

Page 19: Extern 修飾詞

描述文件的位置 make -f 描述文件 若沒有特別指定, make 或尋找檔名為

Makefile 或 makefile 的文件

Page 20: Extern 修飾詞

SIGNAL HANDLING#include<signal.h>

Page 21: Extern 修飾詞

Signal <signal.h> 提供了一個手段

(signal function) ,可以在當程式發生不可預期情況(例如 ctrl+c, 除以 0… 等等)時,作例外處理。 當然,使用的時候就要

include<signal.h>

Page 22: Extern 修飾詞

Signal 常數 Signal Explanation

SIGABRT Abnormal termination of the program (such as a call to function abort). SIGFPE An erroneous arithmetic operation, such as a divide by zero or an operation

resulting in overflow. SIGILL Detection of an illegal instruction. SIGINT Receipt of an interactive attention signal. SIGSEGV An invalid access to storage. SIGTERM A termination request set to the program.

節自上課投影片

Page 23: Extern 修飾詞

Signal 函式 *signal( int signal, void (* func) (int)) Signal 函式會偵測常數為 signal 的狀況,並以 func 例外處理,並回傳函數的指標,如果發生錯誤會回傳 SIG_ERR 。

Page 24: Extern 修飾詞

raise 函式 raise(int signal); 直接造成某種 signal 發生。


Top Related