Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an inline way to mix c and c++ prototypes?

Tags:

c++

c

I want an inline way of specifying which prototypes should be included with c++. For example:

       void ArrayList_insert(ArrayList *arrlst, void *data, int i);
IS_CPP void ArrayList_insert(ArrayList *arrlst, char *data, int i);
IS_CPP void ArrayList_insert(ArrayList *arrlst, Buffer *data, int i);

currently I am doing:

#ifdef __cplusplus
extern "C" {
#endif

....C HEADERS..

#ifdef __cplusplus
}

....C++ HEADERS...

#endif

but its very inconvenient because overloads of the same function are in different places. I could just have 2 different header files, but that is a pain too. Therefore, Im looking for an inline solution like i proposed above. Does anyone know of a way to do that?

like image 857
chacham15 Avatar asked Jul 01 '13 18:07

chacham15


1 Answers

It's easier than you think.

#ifdef __cplusplus
#define IS_C(x)   extern "C" x ;
#define IS_CPP(x) x ;
#else
#define IS_C(x)   x ;
#define IS_CPP(x) 
#endif

With this type of header:

IS_C   (void ArrayList_insert(ArrayList *arrlst, void *data, int i))
IS_CPP (void ArrayList_insert(ArrayList *arrlst, char *data, int i))
IS_CPP (void ArrayList_insert(ArrayList *arrlst, Buffer *data, int i))
like image 108
Mark Lakata Avatar answered Sep 27 '22 17:09

Mark Lakata