Home > Back-end >  typedef declaration contains #define directive alias
typedef declaration contains #define directive alias

Time:11-07

I use two libraries in my project; let's say A and B for the sake of this question. Unfortunately, I ended up in the following situation:

In A.h:

#define ssize_t long

In B.h:

typedef long long ssize_t;

This leads to the following error, if A.h is included (i.e., processed) prior to B.h:

E0084 invalid combination of type specifiers
C2632 '__int64' followed by 'long' is illegal

My Question: What is the recommended way to deal with this situation?

I could make sure B.h is included prior to A.h instead. I could also #undef ssize_t before including B.h. Neither of which is perfect as it would become my responsibility to ensure the ordering of these includes or uglyfy my code respectively.

Update: It's not my code. The first (A.h) seems to be generated from this, the other (B.h) stems from here.

CodePudding user response:

Read the comments around ssize_t in both header files. Those libraries are meant to be tuned for your system. In particular, one supplies a default in case sys/types.h does not define this type.

It is your duty to understand the system of macros that these authors have used, and make a coherent choice.

Unless A is precompiled, it should be harmless replace the #define long by the typedef long long.

CodePudding user response:

In a pinch, edit both headers, remove configuration logic around the problematic typedef/#define, and insert a simple typedef wrapped in #ifndef in both:

#ifndef my_size_t_defined
#define my_size_t_defined
typedef long long ssize_t;
#endif

If you are not afraid, you can try creating sys/types.h with these lines in your system includes directory instead (or adding them to your existing sys/types.h, but I gather your system doesn't have one). Configure the libraries again and see if they pick up the definition.

A less invasive solution would be to create a non-system directory with sys/types.h and configure both libraries such that they pick up that directory (most build systems have a way of adding custom compiler flags via environment variables).

  • Related