原文連結,文章不錯,修改了排版,看起來更加舒服。
關于extern_C
通常,在C語言的頭檔案中經常可以看到類似下面這種形式的代碼:
#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混合程式設計的時候,就可能會出問題。假設在某一個頭檔案中定義了這樣一個函數:
int foo(int a, int b);
而這個函數的實作位于一個.c檔案中,同時,在.cpp檔案中調用了這個函數。那麼,當CPP編譯器編譯這個函數的時候,就有可能會把這個函數名改成_fooii,這裡的ii表示函數的第一參數和第二參數都是整型。而C編譯器卻有可能将這個函數名編譯成_foo。也就是說,在CPP編譯器得到的目标檔案中,foo()函數是由_fooii符号來引用的,而在C編譯器生成的目标檔案中,foo()函數是由_foo指代的。但連接配接器工作的時候,它可不管上層采用的是什麼語言,它隻認目标檔案中的符号。于是,連接配接器将會發現在.cpp中調用了foo()函數,但是在其它的目标檔案中卻找不到_fooii這個符号,于是提示連接配接過程出錯。extern "C" {}這種文法形式就是用來解決這個問題的。本文将以示例對這個問題進行說明。
首先假設有下面這樣三個檔案:
/* 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檔案中:
/* test_extern_c.c */
#include "test_extern_c.h"
int ThisIsTest(int a, int b)
{
return (a + b);
}
可以看到,ThisIsTest()函數的實作非常簡單,就是将兩個參數的相加結果傳回而已。現在,假設要從CPP中調用ThisIsTest()函數:
/* 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連接配接會發生什麼情況:
[[email protected] src]$ gcc -c test_extern_c.c
[[email protected] src]$ g++ main.cpp test_extern_c.o
[[email protected] src]$ ./a.out 4 5
result=9
可以看到,程式沒有任何異常,完全按照預期的方式工作。那麼,如果将test_extern_c.h中的extern "C" {}所在的那幾行注釋掉會怎樣呢?注釋後的test_extern_c.h檔案内容如下:
/* 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檔案:
[[email protected] src]$ gcc -c test_extern_c.c
[[email protected] 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()的引用。
為了更清楚地說明問題的原因,我們采用下面的方式先把目标檔案編譯出來,然後看目标檔案中到底都有些什麼符号:
[[email protected] src]$ gcc -c test_extern_c.c
[[email protected] 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
[[email protected] src]$ g++ -c main.cpp
[[email protected] 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我并不清楚,但這裡并不影響我們的讨論,是以不去管它。顯然,這就是原因的所在,其原理在本文開頭已作了說明。
[[email protected] src]$ gcc -c test_extern_c.c
[[email protected] 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
那麼,為什麼采用了extern "C" {}形式就不會有這個問題呢,我們就來看一下當test_extern_c.h采用extern "C" {}的形式時編譯出來的目标檔案中又有哪些符号:
[[email protected] src]$ g++ -c main.cpp
[[email protected] 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檔案的函數全都括起來。