Includes a C header that declares a variable called "new"?

I am trying to use the OpenCA library (libPKI) in a C ++ application. However, when you include the pki_x509_data_st.h file, the following code fragment is encountered:

typedef struct pki_x509_callbacks_st {

 /* ---------------- Memory Management -------------------- */
 void * (*new)  (void    );
 void   (*free) (void *x );
 void * (*dup)  (void *x );

This will not compile due to the "new" pointer declaration.

How can I make it work?

Update
After renaming a "new" variable, I ran into some new problems ("using typedef name after struct", etc.). I want to avoid changing the old C code too much (changing the library headers somehow makes me nervous), so I decided to create a minimal isolation layer.

+3
4

, , ++. , "".

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

, - , . ...

+2
#define new its_reserved_dammit
#include <pki_x509_data_st.h>
#undef new

, - .

, C, ​​ ++, , extern "C"...

+6

:

#define new mynew
extern "C"
{
#  include "pki_x509_data_st.h"
}
#undef new

What you also have to do is modify the file and send an error along with your patch.

+4
source

This is a reserved word in C ++. Rename it to "myNew" or something like that.

0
source

Source: https://habr.com/ru/post/1741771/


All Articles