How to overcome "u_int8_t vs uint8_t" issue efficiently
I am trying t开发者_运维百科o build a package (libnet) in Solaris and I found that in Solaris there are no u_xxx_t
but uxxx_t
defined in sys/types.h
I had 2 questions:
1-Shouldn't autotools take care of this for me?
2-I guess I'm not the first one facing this (although google was little help) is there a standard/eficient/correct/quick way of overcoming this?
The most reasonable way of overcoming tis is to stick with the standard spelling of the type names (even if that standard is a "future" one for the implementation you are using). C99 introduced a standard nomenclature for such type names and in C99 it is uint8_t
. So, even if you are using a C89/90 compiler I'd suggest you use uint8_t
in your code. If on some platform it is unavailable or spelled differently, you simply introduce a platform-specific typedef name that "converts" the spelling
typedef u_int8_t uint8_t;
For this to work you'll need a header file that is included into every translation unit. Normally, each project has one created specifically for solving issues like this one.
The typename uint8_t
is standard, so I'm not sure where you found u_int8_t
.
This is simple enough that you can do it a fast, dumb way with perl
(or sed, if you must), and fix any minor problems that it causes by hand:
perl -pi.orig -e "s/\bu_(\w+_t)\b/u$1/g" *.c
(This will save the original, unmodified files with the .orig
suffix.)
- No
- Use conditional preprocessor directives i.e.
#define u_xxx_t uxxx_t
or typedef wrapped in a #ifdef block, i.e.typedef u_xxx_t uxxx_t
Thank you all for the answers, I've learned some things. First of all, about the old naming standard it seems that the package itself is old and not very maintained, the homepage seems to be offline. Looking around the code I found the typedefs:
#if (__sun && __SVR4)
/* libnet should be using the standard type names, but in the short term
* define our non-standard type names in terms of the standard names.
*/
#include <inttypes.h>
typedef uint8_t u_int8_t;
typedef uint16_t u_int16_t;
typedef uint32_t u_int32_t;
typedef uint64_t u_int64_t;
#endif
The #if was originaly like this:
#if (__sun__ $$ svr4)
Both of the macros are deffined differently in the system. After the change, it worked fine.
Thanks again!
精彩评论