C和C++混合編譯
通常,在C語言的頭文件中經常可以看到類似下面這種形式的代碼:
[plain] view plaincopyprint?
- #ifdef __cplusplus
- extern "C" {
- #endif
- /**** some declaration or so *****/
- #ifdef __cplusplus
- }
- #endif /* end of __cplusplus */
那么,這種寫法什么用呢?實際上,這是為了讓CPP能夠與C接口而采用的一種語法形式。之所以采用這種方式,是因為兩種語言之間的一些差異所導致的。由于CPP支持多態性,也就是具有相同函數名的函數可以完成不同的功能,CPP通常是通過參數區分具體調用的是哪一個函數。在編譯的時候,CPP編譯器會將參數類型和函數名連接在一起,于是在程序編譯成為目標文件以后,CPP編譯器可以直接根據目標文件中的符號名將多個目標文件連接成一個目標文件或者可執行文件。但是在C語言中,由于完全沒有多態性的概念,C編譯器在編譯時除了會在函數名前面添加一個下劃線之外,什么也不會做(至少很多編譯器都是這樣干的)。由于這種的原因,當采用CPP與C混合編程的時候,就可能會出問題。假設在某一個頭文件中定義了這樣一個函數:
[plain] view plaincopyprint?
- int foo(int a, int b);
首先假設有下面這樣三個文件:
- /* file: test_extern_c.h */
- #ifndef __TEST_EXTERN_C_H__
- #define __TEST_EXTERN_C_H__
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * this is a test function, which calculate
- * the multiply of a and b.
- */
- extern int ThisIsTest(int a, int b);
- #ifdef __cplusplus
- }
- #endif /* end of __cplusplus */
- #endif
在這個頭文件中只定義了一個函數,ThisIsTest()。這個函數被定義為一個外部函數,可以被包括到其它程序文件中。假設ThisIsTest()函數的實現位于test_extern_c.c文件中:
[plain] view plaincopyprint?- /* test_extern_c.c */
- #include "test_extern_c.h"
- int ThisIsTest(int a, int b)
- {
- return (a + b);
- }
可以看到,ThisIsTest()函數的實現非常簡單,就是將兩個參數的相加結果返回而已。現在,假設要從CPP中調用ThisIsTest()函數:
[plain] view plaincopyprint?
- /* main.cpp */
- #include "test_extern_c.h"
- #include <stdio.h>
- #include <stdlib.h>
- class FOO {
- public:
- int bar(int a, int b)
- {
- printf("result=%i\n", ThisIsTest(a, b));
- }
- };
- int main(int argc, char **argv)
- {
- int a = atoi(argv[1]);
- int b = atoi(argv[2]);
- FOO *foo = new FOO();
- foo->bar(a, b);
- return(0);
- }
在這個CPP源文件中,定義了一個簡單的類FOO,在其成員函數bar()中調用了ThisIsTest()函數。下面看一下如果采用gcc編譯test_extern_c.c,而采用g++編譯main.cpp并與test_extern_c.o連接會發生什么情況:
[plain] view plaincopyprint?- [cyc@cyc src]$ gcc -c test_extern_c.c
- [cyc@cyc src]$ g++ main.cpp test_extern_c.o
- [cyc@cyc src]$ ./a.out 4 5
- result=9
- /* test_extern_c.h */
- #ifndef __TEST_EXTERN_C_H__
- #define __TEST_EXTERN_C_H__
- //#ifdef __cplusplus
- //extern "C" {
- //#endif
- /*
- * this is a test function, which calculate
- * the multiply of a and b.
- */
- extern int ThisIsTest(int a, int b);
- //#ifdef __cplusplus
- // }
- //#endif /* end of __cplusplus */
- #endif
除此之外,其它文件不做任何的改變,仍然采用同樣的方式編譯test_extern_c.c和main.cpp文件:
[plain] view plaincopyprint?- [cyc@cyc src]$ gcc -c test_extern_c.c
- [cyc@cyc src]$ g++ main.cpp test_extern_c.o
- /tmp/cca4EtJJ.o(.gnu.linkonce.t._ZN3FOO3barEii+0x10): In function `FOO::bar(int, int)':
- : undefined reference to `ThisIsTest(int, int)'
- collect2: ld returned 1 exit status
在編譯main.cpp的時候就會出錯,連接器ld提示找不到對函數ThisIsTest()的引用。
為了更清楚地說明問題的原因,我們采用下面的方式先把目標文件編譯出來,然后看目標文件中到底都有些什么符號:
[plain] view plaincopyprint?- [cyc@cyc src]$ gcc -c test_extern_c.c
- [cyc@cyc src]$ objdump -t test_extern_c.o
- test_extern_c.o: file format elf32-i386
- SYMBOL TABLE:
- 00000000 l df *ABS* 00000000 test_extern_c.c
- 00000000 l d .text 00000000
- 00000000 l d .data 00000000
- 00000000 l d .bss 00000000
- 00000000 l d .comment 00000000
- 00000000 g F .text 0000000b ThisIsTest
- [cyc@cyc src]$ g++ -c main.cpp
- [cyc@cyc src]$ objdump -t main.o
- main.o: file format elf32-i386
- SYMBOL TABLE:
- 00000000 l df *ABS* 00000000 main.cpp
- 00000000 l d .text 00000000
- 00000000 l d .data 00000000
- 00000000 l d .bss 00000000
- 00000000 l d .rodata 00000000
- 00000000 l d .gnu.linkonce.t._ZN3FOO3barEii 00000000
- 00000000 l d .eh_frame 00000000
- 00000000 l d .comment 00000000
- 00000000 g F .text 00000081 main
- 00000000 *UND* 00000000 atoi
- 00000000 *UND* 00000000 _Znwj
- 00000000 *UND* 00000000 _ZdlPv
- 00000000 w F .gnu.linkonce.t._ZN3FOO3barEii 00000027 _ZN3FOO3barEii
- 00000000 *UND* 00000000 _Z10ThisIsTestii
- 00000000 *UND* 00000000 printf
- 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?
- [cyc@cyc src]$ gcc -c test_extern_c.c
- [cyc@cyc src]$ objdump -t test_extern_c.o
- test_extern_c.o: file format elf32-i386
- SYMBOL TABLE:
- 00000000 l df *ABS* 00000000 test_extern_c.c
- 00000000 l d .text 00000000
- 00000000 l d .data 00000000
- 00000000 l d .bss 00000000
- 00000000 l d .comment 00000000
- 00000000 g F .text 0000000b ThisIsTest
[plain] view plaincopyprint?
- [cyc@cyc src]$ g++ -c main.cpp
- [cyc@cyc src]$ objdump -t main.o
- main.o: file format elf32-i386
- SYMBOL TABLE:
- 00000000 l df *ABS* 00000000 main.cpp
- 00000000 l d .text 00000000
- 00000000 l d .data 00000000
- 00000000 l d .bss 00000000
- 00000000 l d .rodata 00000000
- 00000000 l d .gnu.linkonce.t._ZN3FOO3barEii 00000000
- 00000000 l d .eh_frame 00000000
- 00000000 l d .comment 00000000
- 00000000 g F .text 00000081 main
- 00000000 *UND* 00000000 atoi
- 00000000 *UND* 00000000 _Znwj
- 00000000 *UND* 00000000 _ZdlPv
- 00000000 w F .gnu.linkonce.t._ZN3FOO3barEii 00000027 _ZN3FOO3barEii
- 00000000 *UND* 00000000 ThisIsTest
- 00000000 *UND* 00000000 printf
- 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中間件 條碼系統中間層 物聯網軟件集成