Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ compatibility with older libraries

Tags:

c++

struct

I have a library 1-1.h.

#include <1-1.h>;

Which has a struct:

struct bucket { ... }

Unfortunately this library is 3-party and they have changed the struct bucket to bucket_t { ... } in 1.2. All my code uses bucket but I would also like it compatible with bucket_t.

Is it possible to:

#ifndef bucket
    typedef bucket_t bucket;
#endif

(Code doesn't work but I would like to set bucket to bucket_t if it exists. Thanks.

like image 855
user622469 Avatar asked Oct 22 '22 07:10

user622469


1 Answers

One option is to add your own predefined symbol in your project or makefile that specifies which version you are using. Something like LIBRARY1_1 or LIBRARY1_2. If neither are defined report an error. You can do this by using your own include file like below.

If the header file is different for each version you use...

my1-1.h

#if defined( LIBRARY1_1 )
#include <1-1.h>
#elif defined( LIBRARY1_2 )
#include <1-2.h>
typedef bucket_t bucket
#else
#error Please define LIBRARY1_1 or LIBRARY1_2 before including this file
#endif

If the same filename is used for the header in each version...

my1-1.h

#include <1-1.h>
#if defined( LIBRARY1_1 )
#elif defined( LIBRARY1_2 )
typedef bucket_t bucket
#else
#error Please define LIBRARY1_1 or LIBRARY1_2 before including this file
#endif
like image 178
Captain Obvlious Avatar answered Nov 15 '22 06:11

Captain Obvlious