C Wrapper around C++ with an Interface-Class

Hey Folks,

I need to create a C Wrapper around a C++_Class and in between needs to be an Interface-Class. The Interface-Class is needed, cause there are more C++_Classes which are kinda equal.

Hierarchicaly it would somehow look like this:
1
2
3
4
5
6
=> XY-Process which is calling the CWrapper
==> CMeasureWrapper.c	// <-- CWrapper
===> CMeasureWrapper.h	// <-- CWrapper Header
====> IMeasurement.h	// <-- Interface-Class
=====> CMeasure.cpp	// <-- C++_Class
======> CMeasure.h	// <-- C++_Class-Header 



My Output after compiling.(files will follow)
1>------ Build started: Project: Platform, Configuration: Release Win32 ------
1>  CMeasureWrapper.c
1>d:\projekte\ict\tools\flexbisoncompilation\platform\IMeasurement.h(4): error C2061: syntax error : identifier 'IMeasurement'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\IMeasurement.h(4): error C2059: syntax error : ';'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\IMeasurement.h(5): error C2449: found '{' at file scope (missing function header?)
1>d:\projekte\ict\tools\flexbisoncompilation\platform\IMeasurement.h(9): error C2059: syntax error : '}'
1>d:\projekte\ict\common\globals\includes\stdint.h(161): warning C4005: 'SIZE_MAX' : macro redefinition
1>          C:\Programme\Microsoft Visual Studio 10.0\VC\INCLUDE\limits.h(82) : see previous definition of 'SIZE_MAX'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(8): error C2061: syntax error : identifier 'CRMeasure'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(8): error C2059: syntax error : ';'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(8): error C2059: syntax error : ':'
1>CMeasureWrapper.c(14): error C2065: 'IMeasurement' : undeclared identifier
1>CMeasureWrapper.c(14): error C2065: 'po_measureValues' : undeclared identifier
1>CMeasureWrapper.c(14): warning C4552: '*' : operator has no effect; expected operator with side-effect
1>CMeasureWrapper.c(17): error C2065: 'po_measureValues' : undeclared identifier
1>CMeasureWrapper.c(17): error C2065: 'new' : undeclared identifier
1>CMeasureWrapper.c(17): error C2146: syntax error : missing ';' before identifier 'CRMeasure'
1>CMeasureWrapper.c(17): error C2065: 'CRMeasure' : undeclared identifier
1>CMeasureWrapper.c(24): error C2065: 'po_measureValues' : undeclared identifier
1>CMeasureWrapper.c(24): warning C4047: '==' : 'int' differs in levels of indirection from 'void *'
1>CMeasureWrapper.c(30): error C2065: 'po_measureValues' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========





All files (5 to be exact)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// CMeasureWrapper.c | CWrapper
#include "CMeasureWrapper.h"

uint32_t gu32_objectHandle;
uint32_t gu32_measureType;

int32_t addNewMeasureObject(uint32_t u32_measureObjType)
{
	int32_t  t_status = 0;

	gu32_measureType = u32_measureObjType;
	IMeasurement *po_measureValues;
	
	if(u32_measureObjType == 0)
		{ po_measureValues = new CRMeasure; }
	else
	{
		t_status = 1;
		return t_status;
	}
	
	if(po_measureValues == NULL)
	{
		t_status = 2;
		return t_status;
	}

	gu32_objectHandle = (uint32_t)po_measureValues;

	return t_status;
}

int32_t CMW_setWait(const uint32_t cu32_wait)
{
	int32_t  t_status = 0;

	IMeasurement *po_measureValues = (IMeasurement*)gu32_objectHandle;
	po_measureValues->setWait(cu32_wait);

	return t_status;
}
int32_t CMW_getWait(uint32_t *pu32_wait)
{
	int32_t  t_status = 0;

	if(pu32_wait == NULL)
	{
		t_status = 2;
		return t_status;
	}

	IMeasurement *po_measureValues = (IMeasurement*)gu32_objectHandle;
	*pu32_wait = po_measureValues->getWait();

	return t_status;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// CMeasureWrapper.h | CWrapper-Header
#ifndef CMEASUREWRAPPER_H
#define CMEASUREWRAPPER_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "IMeasurement.h"
#include "CRMeasure.h"

int32_t addNewMeasureObject(uint32_t u32_measureObjType);
int32_t CMW_setWait(const uint32_t cu32_wait);
int32_t CMW_getWait(uint32_t *pu32_wait);

#ifdef __cplusplus
}
#endif

#endif // CMEASUREWRAPPER_H 



1
2
3
4
5
6
7
8
9
10
11
12
13
// IMeasurement.h | Interface-Class
#ifndef IMEASUREMENT_H
#define IMEASUREMENT_H

#include "kt_types.h"

class IMeasurement
{
public:	
	virtual void setWait(const uint32_t cu32_wait) = 0;
	virtual uint32_t getWait() = 0;
};
#endif // ICMEASUREMENT_H 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
// CRMeasure.cpp | C++_Class (1 of 7)
#include "CRMeasure.h"

CRMeasure::CRMeasure() { mu32_wait = 0; }
CRMeasure::~CRMeasure() {}

void CRMeasure::setWait(IN const uint32_t cu32_wait)
{	
	mu32_wait = cu32_wait;
}
uint32_t CRMeasure::getWait()
{
	return mu32_wait;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// CRMeasure.h | C++_Class-Header (1 of 7)
#ifndef CRMEASURE_H
#define CRMEASURE_H

#include "IMeasurement.h"
#include "kt_types.h"

class CRMeasure : public IMeasurement
{
public:
	CRMeasure();
	virtual ~CRMeasure();
	virtual void setWait(IN const uint32_t cu32_wait);
	virtual uint32_t getWait();
	
protected:
	uint32_t	mu32_wait;
};
#endif // CRMEASURE_H 



It's the first time i'll try to build an CWrapper or even a wrapper. So maybe the Project-Properties need to fit as well.


The project itself is called "Platform" with following settings in:
C/C++ -> Advanced -> Compile As -> "Compile as C++ Code (/TP)"

This property on the CMeasureWrapper.c is switched to:
C/C++ -> Advanced -> Compile As -> "Compile as C Code (/TC)"
but only on this file!

I'm not sure if it's necessary, the file is of .c type, so I wasn't sure.


Can anybody tell me what I am doin wrong? Not just depending on the error output, I mean on the whole project. Is there anything else which will not work with such a combination of CWrapper, Interface and C++_Classes?

Hopefully I get a quick answer :)
If there are any questions, just throw them at me ;)

peace Adi,
and sorry if my english is not that well... I'm a damn german kraut ^^



Last edited on
I only took a quick look but, in CMeasureWrapper.h, why are the #includes inside the extern "C"?

Any time I start using extern I always review the following to FAQs:
http://www.stroustrup.com/bs_faq2.html#callC
http://www.stroustrup.com/bs_faq2.html#callCpp

Hope this helps.
Last edited on
Hmm looks nice,
although I tried to only use this example, I didn't managed it to work...

It still tells me something like this as output:
1
2
3
4
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(7): error C2061: syntax error : identifier 'C'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(7): error C2059: syntax error : ';'
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(8): error C2449: found '{' at file scope (missing function header?)
1>d:\projekte\ict\tools\flexbisoncompilation\platform\CRMeasure.h(10): error C2059: syntax error : '}'



http://www.stroustrup.com/bs_faq2.html#callCpp
here I used the second solution with virtual...

need to add those files?

Or can anybody give me a little complete "file" solution of such a szenario? Just with posted code or something...


Topic archived. No new replies allowed.