Standard C Episode 8

來源:互聯網
上載者:User

標籤:

C語言函數和程式結構
通過函數可以把大的計算任務分解成若干個較小任務,從而使得思路更加清晰,同時函數也大大提高了代碼的複用率,提高了工作效率。要注意的是多函數之間應該儘可能地高彙總低耦合。另一方面,一個程式可以儲存在一個或者多個源檔案中。各個檔案可以單獨編譯。

注意:一般地,標頭檔中不會放那些在程式運行時發生具體效應的語句。例如聲明(定義)一個變數的語句是不會放在標頭檔中的。再例如結構體聲明(定義)語句就可以放標頭檔裡,但結構體變數聲明(定義)語句是不會放在標頭檔中的。

make工具可以用來進行專案管理。他可以根據記錄在Makefile檔案中的要求完成整個編譯過程。實際項目中Makefile檔案內容複雜,往往還有可以製作Makefile檔案的工具。
linux開源軟體經典的軟體安裝步驟是直接從軟體來源程式開始的,步驟:
(1)make
(2)make install
(3)make clean

 

1 /*2 Filename:add.h3  */4 #ifndef __ADD_H__5 #define __ADD_H__6 void add(int, int);7 #endif  //__ADD_H__
1 /*2 Filename: add.c3  */4 #include "add.h"5 int result;6 void add(int a , int b)7 {8     result = a + b;9 }
1 /*2 Filename: sub.h3  */4 #if !defined(__SUB_H__)5 #define __SUB_H__6 int sub(int, int);7 #endif  //__SUB_H__
1 /*Filename: sub.c2  */3 #include "sub.h"4 int sub(int a, int b)5 {6     return a - b;7 }

 

 1 /* 2 Filename: main.c 3  */ 4  5 #include "add.h" 6 #include "sub.h" 7  8 #include <stdio.h> 9 extern int result;// 聲明result

10 int main()11 {12 add(10, 10);13 printf("%d + %d = %d \n", 10, 10, result);14 printf("%d - %d = %d \n", 10, 10, sub(10, 10));15 16 return 0;17 }

 

Makefile

 1 a.out : add.o   sub.o main.c 2     gcc -c main.c 3     gcc sub.o add.o main.o 4 add.o:add.c 5  6     gcc -c add.c 7  8 sub.o:sub.c 9     gcc -c sub.c10 clean:11     rm *.o a.out

 

Standard C Episode 8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.