日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++中extern关键字使用 http://blog.csdn.net/sruru/article/details/7951019

發布時間:2024/4/14 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中extern关键字使用 http://blog.csdn.net/sruru/article/details/7951019 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++中extern關鍵字使用

分類: C++ 語法 327人閱讀 評論(0) 收藏 舉報 c++編譯器

chapter 1 . extern關鍵字的作用??

? extern是一個關鍵字,它告訴編譯器存在著一個變量或者一個函數,如果在當前編譯語句的前面中沒有找到相應的變量或者函數,也會在當前文件的后面或者其它文件中定義,來看下面的例子。

???

[cpp] view plaincopy
  • //?extern.cpp?:?Defines?the?entry?point?for?the?console?application.??
  • //??
  • ??
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • extern?int?i;??
  • extern?void?func();??
  • int?_tmain(int?argc,?_TCHAR*?argv[])//typedef?wchar_t?????_TCHAR;#define?_tmain??????wmain??
  • {??
  • ????i?=?0;??
  • ????func();??
  • ????return?0;??
  • }??
  • ??
  • int?i;??
  • ??
  • void?func()??
  • {??
  • ????i++;??
  • ????cout?<<?"i?=?"?<<?i?<<?endl;??
  • }??
  • ?

    ??? 上面代碼中變量i和函數func在文件末尾定義,所以變量需要使用extern關鍵字告訴編譯器,變量在別的地方定義。extern int i我原來以為extern i就可以,結果編譯器報錯,仔細想下確實應該,否則編譯器不知道i是什么類型的數據,又怎么能判斷i = 0是否是一個正確的賦值語句呢?

    ?

    ??? 那么定義在其他文件中的函數和變量,如何通過extern關鍵字調用呢?

    ??? 首先,定義在其它文件中的函數和變量,可以使用兩種方法調用:

    ??????? 一、使用頭文件調用,這時候,函數和變量必須在頭文件中定義和聲明。

    ??????? 二、使用extern關鍵字調用,這時候函數和變量在.cpp或者.c文件中定義和聲明。

    ??? 看下面兩個例子:

    ??? devVar.cpp函數中定義:

    ???????

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • ??
  • int?i;??

  • ?

    ??? extern.cpp中

    [cpp] view plaincopy
  • //?extern.cpp?:?Defines?the?entry?point?for?the?console?application.??
  • //??
  • ??
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • extern?int?i;??
  • extern?void?func();??
  • int?_tmain(int?argc,?_TCHAR*?argv[])//typedef?wchar_t?????_TCHAR;#define?_tmain??????wmain??
  • {??
  • ????i?=?0;??
  • ????func();??
  • ????return?0;??
  • }??
  • ??
  • void?func()??
  • {??
  • ????i++;??
  • ????cout?<<?"i?=?"?<<?i?<<?endl;??
  • }??

  • ?

    ?? 編譯工程,程序輸出:i = 1,這里使用extern關鍵字聲明在其它cpp文件中定義的變量和函數。

    ?

    ??? #include <filensme> --- 將filename文件中的內容插入到新的文件中。

    ??? deVar.h文件中代碼為

    [cpp] view plaincopy
  • #include?<stdio.h>??
  • ??
  • int?i?=?1;??
  • ??
  • void?func()??
  • {??
  • ????printf("%d",i++);??
  • }??
  • ???? 函數func修改全局變量i的值并輸出。

    ??? extern.cpp文件內容為:

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • #include?<stdio.h>??
  • #include?<iostream>??
  • using?namespace?std;??
  • #include?"devVar.h"??
  • //extern?int?i;??
  • //extern?void?func();??
  • ??
  • int?main(void)??
  • {??
  • ????for?(int?x?=?0;x?<?10;?x++)??
  • ????{??
  • ????????func();??
  • ????}??
  • }??
  • 程序輸出1,2,3,4,5,6,7,8,9,10,這里#include <filname.h> 包含定義在其它頭文件中的函數和變量,在來看一個例子。

    ???

    [cpp] view plaincopy
  • //?extern.cpp?:?Defines?the?entry?point?for?the?console?application.??
  • //??
  • ??
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • extern?int?i;??
  • extern?int??func(int);//這里extern必需的,函數定義在其它cpp文件中??
  • [cpp] view plaincopy
  • int?_tmain(int?argc,?_TCHAR*?argv[])//typedef?wchar_t?????_TCHAR;#define?_tmain??????wmain??
  • {??
  • ????i?=?100;??
  • ????func(i);??
  • ????return?0;??
  • }??
  • ??? devVar.cpp文件中內容為:

    ???

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • int?i;??
  • ??
  • int?func(int?a)??
  • {??
  • ????i?=?a;??
  • ????cout?<<?"i?=?"?<<?i?<<?endl;??
  • ????return?0;??
  • }??

  • ??? 這樣,同樣是輸出了i= 100。

    ??? 能夠使用extern引用其它cpp文件中定義的函數說明了一個問題:

    ??? 如果一個工程現編譯cpp文件,在把多個目標文件鏈接成為可執行文件,而兩個或多個文件中,定義了相同的全局變量,那么,程序編譯的時候不會報錯,因為編 譯器單獨編譯每個文件,在鏈接可執行文件的時候,由于多個目標文件中含有相同的全局變量,而生成可執行文件的時候,任何文件中定義的全局變量對其它目標文 件都是可見的,此時由于變量定義沖突而發生錯誤。看下面的代碼:

    ???

    [cpp] view plaincopy
  • //?extern.cpp?:?Defines?the?entry?point?for?the?console?application.??
  • //??
  • ??
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • int?i;??
  • extern?int??func(int);//這里extern是必須的函數定義在別的cpp文件中??
  • int?_tmain(int?argc,?_TCHAR*?argv[])//typedef?wchar_t?????_TCHAR;#define?_tmain??????wmain??
  • {??
  • ????i?=?100;??
  • ????func(i);??
  • ????return?0;??
  • }??

  • ?

    devVar.cpp文件中,內容為:

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • int?i;??
  • ??
  • int?func(int?a)??
  • {??
  • ????i?=?a;??
  • ????cout?<<?"i?=?"?<<?i?<<?endl;??
  • ????return?0;??
  • }??
  • ??? 單獨compile任何一個cpp文件都是對的,但是?編譯工程,生成可執行文件的時候報錯:

    ??? 1>LINK : D:\vctest\extern\Debug\extern.exe not found or not built by the last incremental link; performing full link
    1>devVar.obj : error LNK2005: "int i" (?i@@3HA) already defined in extern.obj
    1>D:\vctest\extern\Debug\extern.exe : fatal error LNK1169: one or more multiply defined symbols found

    ??? 原因是:兩個.cpp文件中都定義了全局變量i,變量重復定義了。

    ?

    ??? PS:定義在.h文件中的函數和變量不能使用extern變量聲明,原因是#include <filename>在預編譯的時候將.h文件中的內容插入了cpp文件中,因此編譯器找得到在其它.h文件中定義的變量或者函數。編譯的時 候,只編譯cpp文件的內容,.h文件時不參與編譯,如果使用extern聲明在.h文件中定義的變量或者函數,那么聲明為extern的變量和函數在其 它.cpp文件中找不到,因此程序編譯的時候就發生了錯誤。

    ?

    ?

    chapter2,如何混合編譯C語言和C++

    ??? 實際開發過程中,C++中會調用C與語言編寫的代碼,我在網絡上面找到一篇寫得很好的文章

    ?? http://blog.csdn.net/keensword/article/details/401114

    ?? 就著上面的例子,我使用C語言采用兩種方法重寫了一下。

    ?? 方法一、全局函數和變量在devVar.c文件中實現,在extern.cpp文件中使用extern關鍵字聲明在devVar.c文件中定義的函數和變量。

    ?? devVar.c文件的代碼如下所示:

    ???

    [cpp] view plaincopy
  • #include?<stdio.h>??
  • ??
  • int?i?=?1;??
  • ??
  • void?func()??
  • {??
  • ????printf("%d",i++);??
  • }??
  • ?? extern.cpp文件中代碼如下所示:

    ???

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • #include?<stdio.h>??
  • #include?<iostream>??
  • using?namespace?std;??
  • //#include?"devVar.h"??
  • //extern?int?i;??
  • //extern?void?func();??
  • ??
  • extern?"C"??
  • {??
  • ????extern?int?i;??
  • ????extern?void?func();??
  • ????//#include?"devVar.h"???
  • }??
  • int?main(void)??
  • {??
  • ????for?(int?x?=?0;x?<?10;?x++)??
  • ????{??
  • ????????func();??
  • ????}??
  • }??
  • ??? 所以在C++文件中編譯C文件需要使用extern "C"關鍵字,聲明語法如下所示

    ??? extern "C"

    ??? {

    ??????? 采用C語言實現的內容

    ??? }

    ?

    ??? 方法二、

    ??? 在devVar.h文件中實現C代碼(即devVar.h作為C語言頭文件),在.cpp文件中包含C語言頭文件。

    ??? devVar.h頭文件內容為:

    ???

    [cpp] view plaincopy
  • #include?<stdio.h>??
  • ??
  • int?i?=?1;??
  • ??
  • void?func()??
  • {??
  • ????printf("%d",i++);??
  • }??
  • ??? extern.cpp文件內容如下所示

    ???

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • #include?<stdio.h>??
  • #include?<iostream>??
  • using?namespace?std;??
  • //#include?"devVar.h"??
  • //extern?int?i;??
  • //extern?void?func();??
  • ??
  • extern?"C"??
  • {??
  • ????//extern?int?i;??
  • ????//extern?void?func();??
  • ????#include?"devVar.h"???
  • }??
  • int?main(void)??
  • {??
  • ????for?(int?x?=?0;x?<?10;?x++)??
  • ????{??
  • ????????func();??
  • ????}??
  • }??
  • ??? 其中,包含C語言頭文件的方式為:

    [cpp] view plaincopy
  • extern?"C"??
  • {??
  • ????//extern?int?i;??
  • ????//extern?void?func();??
  • ????#include?"devVar.h"???
  • }??
  • ?

    ??? 寫到這里,樓主又產生了一個疑問,上面的例子講的是C++調用C實現的代碼,那如果是C調用C++編寫的代碼呢?

    ??? 樓主作了如下改動:

    ??? devVar.cpp代碼為:???

    [cpp] view plaincopy
  • #include?<stdio.h>??
  • ??
  • int?i?=?1;??
  • ??
  • void?func()??
  • {??
  • ????printf("%d",i++);??
  • }??
  • ??? extern.c文件代碼為

    [cpp] view plaincopy
  • #include?<stdio.h>??
  • ??
  • extern?int?i;??
  • extern?void?func();??
  • ??
  • int?main(void)??
  • {??
  • ????int?x?=?0;??
  • ????for?(;x?<?10;?x++)??
  • ????{??
  • ????????func();??
  • ????}??
  • }??
  • ??? 單獨編譯每個文件都通過,鏈接聲稱可執行文件的時候報錯:

    ??? 1>extern.obj : error LNK2019: unresolved external symbol _func referenced in function _main,說明.c文件中extern void func(),按照C編譯的規則,得到函數_func,而devVar.cpp文件采用C++編譯方式,得到的函數為XX·!_func(具體樓主也不知 道哈),這樣鏈接的時候函數自然找不到,那怎么解決呢?

    ??? 需要在devVar.cpp中,明確調用extern "C"關鍵字,聲明cpp文件中有關代碼,需要按照C的方式來生成,修改devVar.cpp文件如下所示:

    ???

    [cpp] view plaincopy
  • ????#include?<stdio.h>??
  • ??
  • ????int?i?=?1;??
  • ??
  • ??
  • extern?"C"?void?func()??
  • ????{??
  • ????????printf("%d",i++);??
  • ????}??
  • ???? 此時,除了需要使用extern "C"聲明編譯的時候采用C方式編譯外,.cpp文件中的代碼可以按照C++方式編寫,例如

    ???? devVar.cpp按照下面方式寫,也是正確的。

    ????

    [cpp] view plaincopy
  • #include?"stdafx.h"??
  • #include?<iostream>??
  • using?namespace?std;??
  • ??
  • int?i?=?1;??
  • ??
  • extern?"C"?void?func()??
  • ????{??
  • ????????cout?<<?"i?=?"?<<?i++?<<?endl;??
  • ????}?
  • 轉載于:https://www.cnblogs.com/bjiang/archive/2013/03/20/2970424.html

    總結

    以上是生活随笔為你收集整理的C++中extern关键字使用 http://blog.csdn.net/sruru/article/details/7951019的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。