首页 > 编程语言 > 详细

C和C++混合编程

时间:2014-01-15 20:18:15      阅读:542      评论:0      收藏:0      [点我收藏+]

一、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++成员函数,我感觉已经失去了面向对象的意义,在此不多介绍。留作以后补充。

C和C++混合编程

原文:http://www.cnblogs.com/drobot/p/3515417.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!