mixing the C and C++

hi all , I am writing a lexer subcomponment and that
subcomponment should output the lex as a C++ object.

But the problem is that lex generates just C source file
and that C file should call some static functions that
defined in some C++ helder file.I just use the
#ifdef _CPLUS_PLUS to turn off the class codes
when compiling the C source file. But those functions are
implements as a C++ source file , Is this possible ?


Is I just use the helder file and just call , is this will work ?
or should I use the keep those functions inside a block
1
2
3
extern "C" {

}


?

I need your ideas____

Thanks in advance.
Can you use flex++ instead?

yes true I can use it but I have spend the days to read and understand the
flex manual. Anyway at least give me a starting point.

However thanks for the idea.
I think it is similar enough to lex that there shouldn't be much of a learning curve.

anyway that is true that I can use the flex++

But I have already finished writing the lex file.It contains a huge amount of
effort then I decided not move to flex++.


However I googled hardly and find the answer for calling C++ functions inside
the C code.

The answer is just use extern "C" and use the #define _CPLUS_PLUS directive.

according to what I read ,you even can call the class member functions inside the C code. But this time I use the calling the C++ function inside C approach.

here is my example code
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


//
//	EventListener.h
//		:This helder file is the interface for the LexerEvents.
//		


// 
// dependency includes
//
//
#include "LexOutput.h"
#include "Torkens.h"
#include "Lexer.h"


#ifndef __EVENT_LISTENER__
#define __EVENT_LISTENER__


//  :KUGLY: we are not using the namespaces here cos we
//	are dealing with those extern "C" functions.


// static functions that our extern "C" code can directly call
#ifdef __CPLUS_PLUS
extern "C" {
#endif

void InsertKeyword ( int torkenId );
void InsertIdentifier ( int torkenId , char * lpszIdentifierName );
void InsertRValue ( int torkenId , char * lpszRValue );
void InsertOperator ( int torkenId );


#ifdef __CPLUS_PLUS
} // end extern "C"
#endif


//
//	start block of the CPP code
//
//

#ifdef _CPLUS_PLUS

// the class definition starts here 
// now we using the namespace
namespace lexer
{
class EventListener
{
private:
	// contains a reference to the LexOutput object
	LexOutput& rLexOutput ;

public:
	// the constructor
	// @parm LexOutput tells the EventListener to put the output inside
	//					this object
	EventListener ();
	
	int SetLexOutput ( LexOutput & rLexOutput );


	// contains the public static variable that that holds the current 
	// listener that should listen the events.
	static EventListener * sCurrentEventListener ;

	// declarations of the listener functions
	void InsertKeyword ( int torkenId );

	void InsertKeyword ( int torkenId , char * value );

	void InsertOperator ( int torkenId );
	
	void InsertRvalue ( int torkenId , char * value );


}; // end of the class EventListener

} 
// end namespace lexer


#endif // endif of the #define _CPLUS_PLUS 

#endif // end __EVENT_LISTENER_



Just define that class as a


no offence jsmith, I just post these information for the benefit of a person who will dig this thread in future for the same problem.


will
Last edited on
All the C++ compilers define the macro __cplusplus, you can use that instead of _CPLUS_PLUS
Topic archived. No new replies allowed.