c++ 函式宣告 & 分離式編譯 & 參數傳遞

來源:互聯網
上載者:User

標籤:增強   理解   源檔案   開始   分離式   style   g++   類型   stream   

 

p186~p188:

函式宣告
1、函數只能定義一次,但是可以聲明多次。

 

2、函數的介面:傳回型別 + 函數名 + 形參類型

 

3、為什麼要在標頭檔中進行函式宣告???在源檔案中定義?暫時理解到,這麼做可以增強可讀性。

 

4、含有函式宣告的標頭檔應該被包含到定義函數的源檔案中。(例如:#include "diy.h")


分離式編譯 & 6.8 & 6.9


1、分離式編譯:把源檔案割成好幾塊放在不同的檔案夾下。最終要產生可執行檔,就必須告訴編譯器源檔案在哪裡。
具體操作如下:

>源檔案內容(三個檔案放在同一個檔案夾下面。)

fact.h

int fact(int val);

fact.cpp

#include "fact.h"int fact(int val){    if (val == 1)        return 1;    return val * fact(val - 1);}

factMain.cpp

#include <iostream>#include "fact.h"using namespace std;int main(){    cout << fact(3) << endl; // output=6    return 0;}

>開始編譯 !

1)一起編譯的方法

$ g++ factMain.cpp fact.cpp -o lovecpp -std=c++11$ lovecpp6

 2)真*分離式編譯

第一步

$ g++ -c factMain.cpp$ g++ -c fact.cpp

執行這兩個操作後會產生fact.o、factMain.o,接下來要把object code(*就是.o檔案)連結成可執行檔。

$ g++ factMain.o fact.o -o lovecpp

執行後產生lovecpp.exe。這樣分幾步的好處是:如果修改了其中一個源檔案,就重新編譯改動的就好了。

 

 

6.10

#include <iostream>using namespace std;void swap(int *p, int *q){    int temp;    temp = *p;    *p = *q;    *q = temp;}int main(){    int a = 3, b = 4;    cout <<    a << " " << b << endl;    swap(a , b);    // 交換之後    cout <<    a << " " << b << endl;    /* output:     3 4     4 3     */    return 0;}

 

c++ 函式宣告 & 分離式編譯 & 參數傳遞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.