C和C++相互调用

C调用C++

1. 普通函数

使用extern声明:

// C++ Code
extern "C" int foo( int x );
int foo( int x )
{
   //...
}

// C Code
int foo( int x );
void cc( int x )
{
    foo( x );
    //...
}

2.调用重载的C++函数

须封装单独的接口共C调用。

// C++ Code
void foo( int x );
void foo( float x );

extern "C" void foo_i( int x )
{
    foo( x );
}
extern "C" void foo_f( float x )
{
    foo( x );
}

// C Code
void foo_i( int x );
void foo_f( float x );
void ccc( int x1, float x2 )
{
    foo_i( x1 );
    foo_f( x2 );
    // ...
} 

3.调用成员函数(包括虚函数)

参考网页:blog.csdn.net/caspiansea/article/details/9676153

本文给出了一种方法。基本思想是,写一个 wrapper文件,把 C类封装起来,对外只提供C语言的接口,和 Ci相关的都在 wrapper的实现文件里实现。

apple.h

#ifndef __APPLE_H__  
#define __APPLE_H__  
class Apple  
{  
public:  
    enum  
    {  
        APPLE_COLOR_RED,  
        APPLE_COLOR_BLUE,  
        APPLE_COLOR_GREEN,  
    };  
  
    Apple();  
    int GetColor(void);  
    void SetColor(int color);  
private:  
    int m_nColor;  
};  
#endif  

apple.cpp:

#include "apple.h"  
Apple::Apple():m_nColor(APPLE_COLOR_RED)  
{  
}  
  
void Apple::SetColor(int color)  
{  
    m_nColor = color;  
}  
  
int Apple::GetColor(void)  
{  
    return m_nColor;  
} 

AppleWrapper.h

#ifndef _APPLE_WRAPPER_H__  
#define _APPLE_WRAPPER_H_  
struct tagApple;  
#ifdef __cplusplus  
extern "C" {  
#endif  
struct tagApple *GetInstance(void);  
void ReleaseInstance(struct tagApple **ppInstance);  
extern void SetColor(struct tagApple *pApple, int color);  
extern int GetColor(struct tagApple *pApple);  
#ifdef __cplusplus  
};  
#endif  
#endif

AppleWrapper.cpp

	#include "AppleWrapper.h"  
	#include "apple.h"  
	  
	#ifdef __cplusplus  
	extern "C" {  
	#endif  
	struct tagApple  
	{  
	    Apple apple;  
	};  
	struct tagApple *GetInstance(void)  
	{  
	    return new struct tagApple;  
	}  
	void ReleaseInstance(struct tagApple **ppInstance)  
	{  
	    delete *ppInstance;  
	    *ppInstance = 0;  
	      
	}  
	void SetColor(struct tagApple *pApple, int color)  
	{  
	    pApple->apple.SetColor(color);  
	}  
	  
	int GetColor(struct tagApple *pApple)  
	{  
	    return pApple->apple.GetColor();  
	}  
	#ifdef __cplusplus  
	};  
	#endif

test.c

    #include "AppleWrapper.h"  
    #include <assert.h>  
      
    int main(void)  
    {  
        struct tagApple * pApple;  
        pApple= GetInstance();  
        SetColor(pApple, 1);  
        int color = GetColor(pApple);  
        printf("color = %d\n", color);  
        ReleaseInstance(&pApple);  
        assert(pApple == 0);  
        return 0;  
    }  

C++ 调用c

C++调用C的函数比较简单,直接使用extern "C" {}告诉编译器用C的规则去调用C函数就可以了。

CAdd.h

int cadd(int x, int y);

CAdd.c

#include "CAdd.h"
#include <stdio.h>

int cadd(int x, int y) {
    printf("from C function.\n");
    return (x + y);

cppmain.cpp

#include <stdio.h>

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

int main()
{
  int sum = cadd(1, 2);
  printf("1+2 = %d\n", sum);
  return 0;
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×