一、C++中引用C语言函数
该函数位于单独的C源文件和头文件中,则在头文件所有宏定义和函数接口前加
1
2
3
4 |
<span style= "color: rgb(255, 0, 0);" >#ifdef</span> __cplusplus <span style= "color: rgb(255, 0, 0);" > extern </span> "C"
{ <span style= "color: rgb(255, 0, 0);" >#endif </span> |
在头文件"#endif"后加
1
2
3
4 |
<span style= "color: rgb(255, 0, 0);" >#ifdef</span> __cplusplus } <span style= "color: rgb(255, 0, 0);" >#endif </span> |
在此以LED.h为例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
<span style= "color: rgb(255, 0, 0);" >#ifndef</span> __LED_H__ <span style= "color: rgb(255, 0, 0);" >#define</span> __LED_H__ <span style= "color: rgb(0, 255, 0);" > /***需要添加的地方***/ </span><span style= "color: rgb(255, 0, 0);" >#ifdef</span> __cplusplus <span style= "color: rgb(255, 0, 0);" > extern </span> "C"
{ <span style= "color: rgb(255, 0, 0);" >#endif </span> <span style= "color: rgb(0, 255, 0);" > /***原文件中的宏定义***/ </span> <span style= "color: rgb(0, 255, 0);" > /***原文件中的函数构件***/ /***需要添加的地方***/ </span><span style= "color: rgb(255, 0, 0);" >#ifdef</span> __cplusplus } <span style= "color: rgb(255, 0, 0);" >#endif</span> <span style= "color: rgb(255, 0, 0);" >#endif</span> <span style= "color: rgb(0, 255, 0);" > //头文件结束 </span> |
二、在C文件中引用C++成员函数
由于在C文件中引用C++成员函数,我感觉已经失去了面向对象的意义,在此不多介绍。留作以后补充。
原文:http://www.cnblogs.com/drobot/p/3515417.html