c/c++混合编程
2017-05-10 09:33
183 查看
http://www.jianshu.com/p/5d2eeeb93590
http://www.cnblogs.com/Yogurshine/p/3913073.html
extern “C”的主要作用就是为了能够正确实现C++代码调用其他C语言代码。加上extern “C”后,会指示编译器这部分代码按C语言的进行编译,而不是C++的。由于C++支持函数重载,因此编译器编译函数的过程中会将函数的参数类型也加到编译后的代码中,而不仅仅是函数名;而C语言并不支持函数重载,因此编译C语言代码的函数时不会带上函数的参数类型,一般只包括函数名。
http://www.cnblogs.com/Yogurshine/p/3913073.html
extern “C”的主要作用就是为了能够正确实现C++代码调用其他C语言代码。加上extern “C”后,会指示编译器这部分代码按C语言的进行编译,而不是C++的。由于C++支持函数重载,因此编译器编译函数的过程中会将函数的参数类型也加到编译后的代码中,而不仅仅是函数名;而C语言并不支持函数重载,因此编译C语言代码的函数时不会带上函数的参数类型,一般只包括函数名。
c/c++混合编程 1.C++调用C函数 共有三个文件:1.h 1.c main.cpp [root@localhost aa]#cat 1.h #ifndef _1__H_ #define _1__H_ extern void print(char* ); #endif [root@localhost aa]#cat 1.c #include <stdio.h> #include "1.h" void print(char* data) { printf("%s\n", data); } [root@localhost aa]#cat main.cpp extern "C"{ #include "1.h"} int main(void) { print(“hello,world\n”); return 0; } gcc –c 1.c g++ main.cpp 1.o C调用C++,使用extern "C"则是告诉编译器依照C的方式来编译封装接口,当然接口函数里面的C++语法还是按C++方式编译。 2.C调用C++函数(接口) 1.设计程序,共四个文件 animal.cpp animal.h main.c Makefile 1.1 animal.h [root@localhost CC++]#cat animal.h #ifndef __ANIMAL_H__ //防止被重复包含 #define __ANIMAL_H__ #ifdef __cplusplus extern "C" { #endif class ANIMAL{ public: ANIMAL(char* ); ~ANIMAL(); char* getname(void); private: char* name; }; void print(void); #ifdef __cplusplus } #endif #endif // __ANIMAL_H__ 1.2 animal.cpp:C++文件 [root@localhost CC++]#cat animal.cpp #include "animal.h" #include <iostream> using namespace std; ANIMAL::ANIMAL(char* data)//构造函数 { name = new char[64]; strcpy(name, data); } ANIMAL::~ANIMAL() //析构函数 { if(name) { delete[] name; name = NULL; } } char* ANIMAL::getname(void) { return name; } void print(void) //对外接口,而且必须有一个非类中方法,才能被C调用 { ANIMAL animal("dog"); char* animal_name = animal.getname(); cout << "animal name is :" << animal_name << endl; } 1.3 main.c:C文件 [root@localhost CC++]#cat main.c int main(void) { print(); return 0; } 1.4 Makefile [root@localhost CC++]#cat Makefile main:main.c animal.o gcc -lstdc++ main.c animal.o -o main animal.o:animal.h g++ -c animal.cpp .PHONY : clean clean: -rm animal.o main 2.测试 2.1生成可执行程序main [root@localhost CC++]#make g++ -c animal.cpp gcc -lstdc++ main.c animal.o -o main 2.2运行可执行程序main [root@localhost CC++]# ./main animal name is :dog 误区二 : 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++
相关文章推荐
- c&c++混合编程的注意事项
- Python 与 C++ 混合编程 实验
- C++混合编程之idlcpp教程Python篇(9)
- Swift与OC,C++混合编程
- 在Qt(C++)中与Python混合编程
- C和C++混合编程中的extern "C" {}
- Matlab & C++ 混合编程mex文件的编写与调试
- matlab与C++混合编程 matlab发布、打包DLL
- C/C++/Go混合编程实践之GoJieba
- 关于swift,OC,C++混合编程(一)
- Object-C与标准C/C++混合编程(太利害啦!)
- c与c++混合编程
- matcom进行matlab与c++混合编程
- 关于matlab与C++混合编程
- C/C++混合编程与DLL库导出
- QT开发(六十九)——QML与C++混合编程
- [转]Linux下Python与C++混合编程
- 使用JNI进行Java与C/C++语言混合编程(1)--在Java中调用C/C++本地库
- 关于C++与Matlab混合编程错误??? Unexpected Standard exception from MEX file.
- SWIG - C++同C#的混合编程(二)