Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4974690
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: C/C++

2010-07-16 20:23:53

// cpp.h
#ifndef  __cpp_h__
#define  __cpp_h__

class  class1 {
    class1();
    ~class1();
public:
    int  I;
    int  J;

    int  getI(void);
};

#endif
// end file

// cpp.cpp
#i nclude "stdafx.h"
#i nclude 
#i nclude  "cpp.h"
#i nclude  "c.h"

using namespace  std;       // 打开标准库名字空间

class1::class1()
{}

class1::~class1()
{}

int  class1::getI(void)
{
    return  I++;
}

// 按 C 调用方式编译下面函数
extern "C"
int  get_class1_I(struct1 * p)
{
    class1 * pClass1 = (class1 *)p;

    cout << "c++: " << pClass1->getI() << endl;

    return  pClass1->getI();
}

// end file

// c.h
#ifndef  __c_h__
#define  __c_h__

#ifdef  __cplusplus
extern "C" {
#endif

    typedef struct {
        int  i;             // 与 class1 类中变量一致
  int  j;
    }struct1;

#ifdef  __cplusplus
}
#endif
#endif
// end file

// c.c
#i nclude 
#i nclude  "c.h"

extern  int  get_class1_I(void * p);

struct1  s;

int  main(void)
{
    printf ("c: %d\n", get_class1_I(&s));
    printf ("c: %d\n", get_class1_I(&s));
 
    return 0;
}

// end file

参考了eCos中的混和编程实现方式()。

本例在ADS 1.2中编译通过,执行结果正确。
VC++中编译时,C.C文件编译选项中选择 Not using precompile headers。

阅读(635) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~