The C ++ keyword 'new' and C

Possible duplicate:
Use the class keyword as a variable name in C ++

In the C library header file, I use one of the variables named "new". Unfortunately, I use this library in a C ++ project, and the appearance of "new" as variable names leads to a compromise. I already use extern "C" {# include <...>}, but this does not seem to help in this regard.

I need the library developer to change the name of this variable, although from his point of view, as the developer of C, the code is absolutely right, since the “new” is not a C keyword?

+5
source share
4 answers

Before including a header file, use a preprocessor to rename new:

#define new mynew
#include <...>
#undef new

This will allow compilation to continue.

Do you really need to access this variable? If not, then everything is ready. If so, then you need to make sure that the .c files for the library are compiled with

-Dnew=mynew
+7
source

, ? "new", , , , . , - "", . , .C-, .C "private" , .

.C , , "new", .

, .

, , "" -, , "was_new". C #define new was_new.

+3

- C. , , , C ++. , :

other_lib.h:
int foo( int new );

my_app.cxx:
extern "C" {
#include <other_lib.h>
}

, :

my_wrap.h:
#ifdef __cplusplus
extern "C" {
#endif
int my_foo( int );
#ifdef __cplusplus
}
#endif

my_wrap.c:
#include <other_lib.h>
int my_foo( int x ) { return foo( x ); }

my_app.cxx:
#include "my_wrap.h"

...

my_wrap.c C, my_app.cxx ++. , .

+1

, . ++ C, , , C "", ++.

:

++ (*.cpp), #include ( " " ..) .

, ,

0

All Articles