<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    C和C++混合編譯

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接 關于extern_C

     

    通常,在C語言的頭文件中經常可以看到類似下面這種形式的代碼:

     

    [plain] view plaincopyprint?  
    1. #ifdef  __cplusplus  
    2. extern "C" {  
    3. #endif  
    4.   
    5.   
    6. /**** some declaration or so *****/  
    7.   
    8.    
    9. #ifdef  __cplusplus  
    10. }  
    11. #endif  /* end of __cplusplus */  

     

    那么,這種寫法什么用呢?實際上,這是為了讓CPP能夠與C接口而采用的一種語法形式。之所以采用這種方式,是因為兩種語言之間的一些差異所導致的。由于CPP支持多態性,也就是具有相同函數名的函數可以完成不同的功能,CPP通常是通過參數區分具體調用的是哪一個函數。在編譯的時候,CPP編譯器會將參數類型和函數名連接在一起,于是在程序編譯成為目標文件以后,CPP編譯器可以直接根據目標文件中的符號名將多個目標文件連接成一個目標文件或者可執行文件。但是在C語言中,由于完全沒有多態性的概念,C編譯器在編譯時除了會在函數名前面添加一個下劃線之外,什么也不會做(至少很多編譯器都是這樣干的)。由于這種的原因,當采用CPP與C混合編程的時候,就可能會出問題。假設在某一個頭文件中定義了這樣一個函數:

     

    [plain] view plaincopyprint?  
    1. int foo(int a, int b);  
    而這個函數的實現位于一個.c文件中,同時,在.cpp文件中調用了這個函數。那么,當CPP編譯器編譯這個函數的時候,就有可能會把這個函數名改成_fooii,這里的ii表示函數的第一參數和第二參數都是整型。而C編譯器卻有可能將這個函數名編譯成_foo。也就是說,在CPP編譯器得到的目標文件中,foo()函數是由_fooii符號來引用的,而在C編譯器生成的目標文件中,foo()函數是由_foo指代的。但連接器工作的時候,它可不管上層采用的是什么語言,它只認目標文件中的符號。于是,連接器將會發現在.cpp中調用了foo()函數,但是在其它的目標文件中卻找不到_fooii這個符號,于是提示連接過程出錯。extern "C" {}這種語法形式就是用來解決這個問題的。本文將以示例對這個問題進行說明。

     


    首先假設有下面這樣三個文件:

    [plain] view plaincopyprint?  
    1.  /* file: test_extern_c.h */  
    2.   
    3. #ifndef __TEST_EXTERN_C_H__  
    4.   
    5. #define __TEST_EXTERN_C_H__  
    6.   
    7. #ifdef  __cplusplus  
    8. extern "C" {  
    9. #endif  
    10.   
    11. /*  
    12.  * this is a test function, which calculate  
    13.  * the multiply of a and b.  
    14.  */  
    15.   
    16. extern int ThisIsTest(int a, int b);  
    17.   
    18. #ifdef  __cplusplus  
    19. }  
    20. #endif  /* end of __cplusplus */  
    21.   
    22. #endif  

     

    在這個頭文件中只定義了一個函數,ThisIsTest()。這個函數被定義為一個外部函數,可以被包括到其它程序文件中。假設ThisIsTest()函數的實現位于test_extern_c.c文件中:

    [plain] view plaincopyprint?  
    1. /* test_extern_c.c */  
    2.   
    3. #include "test_extern_c.h"  
    4.   
    5. int ThisIsTest(int a, int b)  
    6. {  
    7.     return (a + b);  
    8. }  

     

    可以看到,ThisIsTest()函數的實現非常簡單,就是將兩個參數的相加結果返回而已。現在,假設要從CPP中調用ThisIsTest()函數:

     

    [plain] view plaincopyprint?  
    1. /* main.cpp */  
    2. #include "test_extern_c.h"  
    3. #include <stdio.h>  
    4. #include <stdlib.h>  
    5.   
    6. class FOO {  
    7. public:  
    8.   
    9. int bar(int a, int b)  
    10. {  
    11.     printf("result=%i\n", ThisIsTest(a, b));  
    12. }  
    13. };  
    14.   
    15. int main(int argc, char **argv)  
    16. {  
    17.     int a = atoi(argv[1]);  
    18.     int b = atoi(argv[2]);   
    19.   
    20.     FOO *foo = new FOO();  
    21.   
    22.     foo->bar(a, b);  
    23.   
    24.     return(0);  
    25. }  

     

    在這個CPP源文件中,定義了一個簡單的類FOO,在其成員函數bar()中調用了ThisIsTest()函數。下面看一下如果采用gcc編譯test_extern_c.c,而采用g++編譯main.cpp并與test_extern_c.o連接會發生什么情況:

    [plain] view plaincopyprint?  
    1. [cyc@cyc src]$ gcc -c test_extern_c.c  
    2.   
    3. [cyc@cyc src]$ g++ main.cpp test_extern_c.o  
    4.   
    5. [cyc@cyc src]$ ./a.out 4 5                  
    6.   
    7. result=9  
    可以看到,程序沒有任何異常,完全按照預期的方式工作。那么,如果將test_extern_c.h中的extern "C" {}所在的那幾行注釋掉會怎樣呢?注釋后的test_extern_c.h文件內容如下: [plain] view plaincopyprint?  
    1.  /* test_extern_c.h */  
    2. #ifndef __TEST_EXTERN_C_H__  
    3. #define __TEST_EXTERN_C_H__  
    4.   
    5. //#ifdef   __cplusplus  
    6. //extern "C" {  
    7. //#endif  
    8.   
    9. /*  
    10.  * this is a test function, which calculate  
    11.  * the multiply of a and b.  
    12.  */  
    13.   
    14. extern int ThisIsTest(int a, int b);  
    15.    
    16.   
    17. //#ifdef   __cplusplus  
    18. //  }  
    19. //#endif   /* end of __cplusplus */  
    20.   
    21. #endif  

    除此之外,其它文件不做任何的改變,仍然采用同樣的方式編譯test_extern_c.c和main.cpp文件:

    [plain] view plaincopyprint?  
    1. [cyc@cyc src]$ gcc -c test_extern_c.c  
    2.   
    3. [cyc@cyc src]$ g++ main.cpp test_extern_c.o  
    4.   
    5. /tmp/cca4EtJJ.o(.gnu.linkonce.t._ZN3FOO3barEii+0x10): In function `FOO::bar(int, int)':  
    6.   
    7. : undefined reference to `ThisIsTest(int, int)'  
    8.   
    9. collect2: ld returned 1 exit status  

     

    在編譯main.cpp的時候就會出錯,連接器ld提示找不到對函數ThisIsTest()的引用。

    為了更清楚地說明問題的原因,我們采用下面的方式先把目標文件編譯出來,然后看目標文件中到底都有些什么符號:

    [plain] view plaincopyprint?  
    1. [cyc@cyc src]$ gcc -c test_extern_c.c      
    2.   
    3. [cyc@cyc src]$ objdump -t test_extern_c.o  
    4.   
    5. test_extern_c.o:     file format elf32-i386  
    6.   
    7. SYMBOL TABLE:  
    8.   
    9. 00000000 l    df *ABS*  00000000 test_extern_c.c  
    10.   
    11. 00000000 l    d  .text  00000000  
    12.   
    13. 00000000 l    d  .data  00000000  
    14.   
    15. 00000000 l    d  .bss   00000000  
    16.   
    17. 00000000 l    d  .comment       00000000  
    18.   
    19. 00000000 g     F .text  0000000b ThisIsTest  
    20.    
    21.   
    22. [cyc@cyc src]$ g++ -c main.cpp            
    23.   
    24. [cyc@cyc src]$ objdump -t main.o          
    25.    
    26.   
    27. main.o:     file format elf32-i386  
    28.   
    29. SYMBOL TABLE:  
    30.   
    31. 00000000 l    df *ABS*  00000000 main.cpp  
    32.   
    33. 00000000 l    d  .text  00000000  
    34.   
    35. 00000000 l    d  .data  00000000  
    36.   
    37. 00000000 l    d  .bss   00000000  
    38.   
    39. 00000000 l    d  .rodata        00000000  
    40.   
    41. 00000000 l    d  .gnu.linkonce.t._ZN3FOO3barEii 00000000  
    42.   
    43. 00000000 l    d  .eh_frame      00000000  
    44.   
    45. 00000000 l    d  .comment       00000000  
    46.   
    47. 00000000 g     F .text  00000081 main  
    48.   
    49. 00000000         *UND*  00000000 atoi  
    50.   
    51. 00000000         *UND*  00000000 _Znwj  
    52.   
    53. 00000000         *UND*  00000000 _ZdlPv  
    54.   
    55. 00000000  w    F .gnu.linkonce.t._ZN3FOO3barEii 00000027 _ZN3FOO3barEii  
    56.   
    57. 00000000         *UND*  00000000 _Z10ThisIsTestii  
    58.   
    59. 00000000         *UND*  00000000 printf  
    60.   
    61. 00000000         *UND*  00000000 __gxx_personality_v0  

     

    可以看到,采用gcc編譯了test_extern_c.c之后,在其目標文件test_extern_c.o中的有一個ThisIsTest符號,這個符號就是源文件中定義的ThisIsTest()函數了。而在采用g++編譯了main.cpp之后,在其目標文件main.o中有一個_Z10ThisIsTestii符號,這個就是經過g++編譯器“粉碎”過后的函數名。其最后的兩個字符i就表示第一參數和第二參數都是整型。而為什么要加一個前綴_Z10我并不清楚,但這里并不影響我們的討論,因此不去管它。顯然,這就是原因的所在,其原理在本文開頭已作了說明。

     

    [plain] view plaincopyprint?  
    1. [cyc@cyc src]$ gcc -c test_extern_c.c  
    2.   
    3. [cyc@cyc src]$ objdump -t test_extern_c.o  
    4.   
    5. test_extern_c.o:     file format elf32-i386  
    6.   
    7. SYMBOL TABLE:  
    8.   
    9. 00000000 l    df *ABS*  00000000 test_extern_c.c  
    10.   
    11. 00000000 l    d  .text  00000000  
    12.   
    13. 00000000 l    d  .data  00000000  
    14.   
    15. 00000000 l    d  .bss   00000000  
    16.   
    17. 00000000 l    d  .comment       00000000  
    18.   
    19. 00000000 g     F .text  0000000b ThisIsTest  
    那么,為什么采用了extern "C" {}形式就不會有這個問題呢,我們就來看一下當test_extern_c.h采用extern "C" {}的形式時編譯出來的目標文件中又有哪些符號:

     

     

    [plain] view plaincopyprint?  
    1. [cyc@cyc src]$ g++ -c main.cpp  
    2.   
    3. [cyc@cyc src]$ objdump -t main.o  
    4.   
    5. main.o:     file format elf32-i386  
    6.   
    7. SYMBOL TABLE:  
    8.   
    9. 00000000 l    df *ABS*  00000000 main.cpp  
    10.   
    11. 00000000 l    d  .text  00000000  
    12.   
    13. 00000000 l    d  .data  00000000  
    14.   
    15. 00000000 l    d  .bss   00000000  
    16.   
    17. 00000000 l    d  .rodata        00000000  
    18.   
    19. 00000000 l    d  .gnu.linkonce.t._ZN3FOO3barEii 00000000  
    20.   
    21. 00000000 l    d  .eh_frame      00000000  
    22.   
    23. 00000000 l    d  .comment       00000000  
    24.   
    25. 00000000 g     F .text  00000081 main  
    26.   
    27. 00000000         *UND*  00000000 atoi  
    28.   
    29. 00000000         *UND*  00000000 _Znwj  
    30.   
    31. 00000000         *UND*  00000000 _ZdlPv  
    32.   
    33. 00000000  w    F .gnu.linkonce.t._ZN3FOO3barEii 00000027 _ZN3FOO3barEii  
    34.   
    35. 00000000         *UND*  00000000 ThisIsTest  
    36.   
    37. 00000000         *UND*  00000000 printf  
    38.   
    39. 00000000         *UND*  00000000 __gxx_personality_v0  

     

    注意到這里和前面有什么不同沒有,可以看到,在兩個目標文件中,都有一個符號ThisIsTest,這個符號引用的就是ThisIsTest()函數了。顯然,此時在兩個目標文件中都存在同樣的ThisIsTest符號,因此認為它們引用的實際上同一個函數,于是就將兩個目標文件連接在一起,凡是出現程序代碼段中有ThisIsTest符號的地方都用ThisIsTest()函數的實際地址代替。另外,還可以看到,僅僅被extern "C" {}包圍起來的函數采用這樣的目標符號形式,對于main.cpp中的FOO類的成員函數,在兩種編譯方式后的符號名都是經過“粉碎”了的。

     

    因此,綜合上面的分析,我們可以得出如下結論:采用extern "C" {} 這種形式的聲明,可以使得CPP與C之間的接口具有互通性,不會由于語言內部的機制導致連接目標文件的時候出現錯誤。需要說明的是,上面只是根據我的試驗結果而得出的結論。由于對于CPP用得不是很多,了解得也很少,因此對其內部處理機制并不是很清楚,如果需要深入了解這個問題的細節請參考相關資料。

     

    備注:

    1. 對于要在cpp中使用的在c文件中寫好的函數func(),只需要在c文件的頭文件中添加extern "C"聲明就可以了。比如:extern "C" func() { ...}

    當然,可以使用

    #ifdef __cplusplus

    extern "C" {

    #endif

     

    #ifdef __cplusplus

    }

    #endif

    將整個c文件的函數全都括起來。RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全