C++調用C的一般寫法

參考文章一:http://blog.163.com/sean_1010/blog/static/11080322200952633111975/
參考文章二:http://songpengfei.iteye.com/blog/1100239

C++調用C的一般寫法

1.C的頭文件

// module_c.h
#ifndef MODULE_C_H
#define MODULE_C_H

#ifdef __cplusplus
extern "C"
{
#endif

extern int add(int x, int y);

#ifdef __cplusplus
};
#endif

#endif

2.C的源文件

// module_c.c
#include "module_c.h"

int add(int x, int y)
{
    return a + b;
}

3.調用C的C++的源文件

// module_cpp.cpp
#include "module_c.h"
#include <iostream>

using namespace std;

int main()
{
    cout << add(1,3) << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章