- /*=======sum.h=========*/
- #ifndef SUM_H #define SUM_H
- #include <stdio.h>
- int sum(int a,int b);
- #endif;
- /*=======sum.cpp=========*/
- #include "sum.h"
- extern "C" {
- int sum(int a,int b) {
- int c=a+b; return c;
- } }
- /*====main.c======*/
- #include "sum.h"
- void mian(){
- cout << sum(1,2)<<endl;
- }
3. 标准规范写法
一般我们都将函数声明放在头文件,当我们的函数有可能被C或C++使用时,我们无法确定被谁调用,使得不能确定是否要将函数声明在extern "C"里,所以,我们可以添加
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- //函数声明
- #ifdef __cplusplus
- }
- #endif










