首先,混合编程不是指在同一个文件里写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
- 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./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,二者等价。
|