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 <
-
#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
-
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++