CとC++コードの相互呼び出し

7149 ワード

1、C++コード呼び出しCコード
//c.h
#ifndef __C_H__
#define __C_H__

#include 

int Add(int a, int b);

#endif
//c.c
#include "c.h"

int Add(int a, int b)
{
	return a + b;
}
//cpp.cpp
#include 
using namespace std;

extern "C"
{
	#include "c.h"
}

int main()
{
	cout << Add(1, 2) << endl;
	system("pause");
	return 0;
}

2、Cコード呼び出しC++コード
//cpp.h
#ifndef __CPP_H__
#define __CPP_H__

#include 
using namespace std;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int Add(int a, int b);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif
//cpp.cpp
#include "cpp.h"

int Add(int a, int b)
{
	cout << a + b << endl;
	return 0;
}
//c.c
#include 

extern int Add(int a, int b);

int main()
{
	Add(1, 2);
	system("pause");
	return 0;
}