3

I'm trying to use the OpenCA (libPKI) library in a C++ application. However, when including the file pki_x509_data_st.h 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 won't compile because of the "new" pointer declaration.

How can I make it work?

Update
After renaming the "new" variable I've encountered some new problems ("using typedef name after struct" etc.. ). I want to avoid changing too much of the old C code (modifying library headers somehow makes me feel nervous) so I decided just create a minimal isolation layer instead.

Mat
  • 202,337
  • 40
  • 393
  • 406
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278

4 Answers4

6
#define new its_reserved_dammit
#include <pki_x509_data_st.h>
#undef new

I suspect that the linker will be upset about it, though.

However, using a C library which isn't prepared for usage from C++, the linker will complain anyway, since the headers are missing the important extern "C" wrappers anyway...

sbi
  • 219,715
  • 46
  • 258
  • 445
4

Try this:

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

What you should also do is change the file and submit a bug together with your patch.

Eddy Pronk
  • 6,527
  • 5
  • 33
  • 57
2

If you can get away with it, rename it to something that's not a reserved word in C++. Chances that that you might end up having to rebuild the whole library and apply your "fix" there as well.

I'd be looking into constructing an isolation layer between your C++ code and the C library, potentially a bit of C code that only exposes the functions you need to use and completely isolate you from the rest.

Oh, and by the way - that's a function pointer declaration, not a variable declaration. Just to clear this up...

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
0

It's a reserved word in C++. Rename it to "myNew" or something.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74