天天看點

linux下C與C++混合程式設計

首先,混合程式設計不是指在同一個檔案裡寫C與C++。 比如說想在同意檔案裡同時 弱類型檢查,又強類型檢查,那真夠嗆。

混合程式設計包括:1,C++引用C的頭檔案;2,g++生成的.o與gcc生成的.o相連結。

1,在用C語言寫的頭檔案中,經常加上如下 保護宏定義:

/*

example.h

*/

#ifndef EXAMPLE_H_

#define EXAMPLE_H_

#ifdef __cplusplus

extern "C"{

#endif

/*這裡是頭檔案内容*/

/*頭檔案内容結束*/

#ifdef __cplusplus

}

#endif

#endif

2,關于extern "C"

          當c++引用c的函數,結構體定義等時,要聲明extern "C"  從某種意義上,這倒是像是在C++檔案裡寫C的一種方法。事實上,由于c++會将函數标示符進行修飾後使用,而C不會,是以用C++編譯的fun有可能是fun_int,這樣在連結時會出現問題。使用extern “C”來解決這一問題,但帶來的影響是不能重載了。

         總之,extern "C"即叫編譯器按照C的方式去處理。

3,具體編譯指令 g++ 與 gcc

執行個體1:

//test.c

int fun(int a, int b)

{

      return a+b;

}

//main.cpp

#include <stdio.h>

#include "test.c"

int main()

{

      printf("%d/n", fun(10, 11));

      return 0;

}

首先了解include,include會把包含的檔案直接加在本檔案中,和copy過來一樣。而我們直接包含了test.c檔案,而不是頭檔案,

是以,直接:   g++ -o main main.cpp  即可得到可執行檔案。

執行個體2:

//test.c

//test.c

int fun(int a, int b)

{

return a+b;

}

//test.h

int fun(int, int);

//main.cpp

#include <stdio.h>

#include "test.h"

int main()

{

printf("%d/n", fun(10, 11));

return 0;

}

正确的編譯方法:

            g++ -c test.c                 //生成test.o

            g++ -c main.cpp          //生成main.cpp

            g++ -o main test.o main.o  //連結,生成main可執行檔案

錯誤的編譯方法:

            gcc -c test.c                 //生成test.o

            g++ -c main.cpp          //生成main.cpp

            g++ -o main test.o main.o  //連結,生成main可執行檔案

   如果,想使第二種方法正确,則在test.h中使用extern “C”聲明,或者在main.cpp中,使用extern "C"聲明

執行個體3

正确寫法1

//test.c

int fun(int a, int b)

{

return a+b;

}

//test.h

#ifdef __cplusplus

extern "C"{

#endif

int fun(int, int);

#ifdef __cplusplus

}

#endif

//main.cpp

#include <stdio.h>

#include "test.h"

int main()

{

printf("%d/n", fun(10, 11));

return 0;

}

正确寫法2

//test.c

int fun(int a, int b)

{

return a+b;

}

//test.h

int fun(int, int);

//main.cpp

#include <stdio.h>

extern "C"

{

#include "test.h"

}

int main()

{

printf("%d/n", fun(10, 11));

return 0;

}

正确寫法3

//test.c

int fun(int a, int b)

{

return a+b;

}

//test.h

int fun(int, int);

//main.cpp

#include <stdio.h>

extern "C" int fun(int, int)

int main()

{

printf("%d/n", fun(10, 11));

return 0;

}

其中正确寫法3很有意思,在main.cpp中,并沒有包含test.h, 這裡寫的extern "C" int fun(int, int),其實就是頭檔案内容。把頭檔案内容人工手寫在main.cpp中和用include包含進來,是一樣效果,這樣就好了解了。 include“test.h” ,其實也就是寫了一句 extern "C" int fun(int, int)。

是以嚴格來說,.h檔案無所謂是屬于C還是C++,被誰包含,就屬于那種語言。

4, 關于g++,gcc

直接引用http://blog.ednchina.com/olivernie/161559/message.aspx上的原文,這段話是轉載,不是我寫的。

在國内搜尋引擎搜尋gcc與g++,大多都是在說g++會調用gcc來編譯c++檔案,國外stackoverflow大多再說gcc調用g++。

有争議,先mark

gcc和g++都是GNU(組織)的一個編譯器。  

  誤區一 :    gcc隻能編譯c代碼,g++隻能編譯c++代碼  

  兩者都可以,但是請注意:  

  1.字尾為.c的,gcc把它當作是C程式,而g++當作是c++程式;字尾為.cpp的,兩者都會認為是c++程式,注意,雖然c++是c的超集,但是兩者對文法的要求是有差別的。C++的文法規則更加嚴謹一些。  

  2.編譯階段,g++會調用gcc,對于c++代碼,兩者是等價的,但是因為gcc指令不能自動和C++程式使用的庫聯接,是以通常用g++來完成連結,為了統一起見,幹脆編譯/連結統統用g++了,這就給人一種錯覺,好像cpp程式隻能用g++似的。  

  誤區二  :  gcc不會定義__cplusplus宏,而g++會  

  實際上,這個宏隻是标志着編譯器将會把代碼按C還是C++文法來解釋,如上所述,如果字尾為.c,并且采用gcc編譯器,則該宏就是未定義的,否則,就是已定義。  

  誤區三 :   編譯隻能用gcc,連結隻能用g++  

  嚴格來說,這句話不算錯誤,但是它混淆了概念,應該這樣說:編譯可以用gcc/g++,而連結可以用g++或者gcc   -lstdc++。因為gcc指令不能自動和C++程式使用的庫聯接,是以通常使用g++來完成聯接。但在編譯階段,g++會自動調用gcc,二者等價。

用gcc編譯c++檔案:

#gcc -o test.exe test.cpp -lstdc++

繼續閱讀