How to successfully solve the problem "u_int8_t vs uint8_t"

I am trying to create a package (libnet) on Solaris, and I found that on Solaris there is no u_xxx_t , but uxxx_t defined in sys / types.h

I had 2 questions:

1 - Shouldn't autotrons take care of this for me?

2-I guess I'm not the first one I came across (although Google has helped a little) is there a standard / eficient / correct / quick way to overcome this?

+4
source share
4 answers

The most sensible way to overcome tis is to stick to the standard spelling of type names (even if that standard is β€œfuture” for the implementation you are using). C99 introduced the standard nomenclature for such type names, and on C99, uint8_t . That way, even if you use the C89 / 90 compiler, I would suggest you use uint8_t in your code. If it is unavailable or written differently on any platform, you simply enter the platform-specific name typedef, which "converts" the spelling

 typedef u_int8_t uint8_t; 

To do this, you will need a header file that will be included in each translation unit. Typically, each project is designed specifically to solve problems such as this one.

+15
source

The typical uint8_t name is standard, so I'm not sure where you found u_int8_t .

It's simple enough that you can do it in a quick and dumb way with perl (or sed if necessary), and fix any minor issues that it causes manually:

 perl -pi.orig -e "s/\bu_(\w+_t)\b/u$1/g" *.c 

(This will save the original, unmodified files with the suffix .orig .)

+6
source
  • Not
  • Use conditional preprocessor directives, i.e. #define u_xxx_t uxxx_t or typedef wrapped in #ifdef block, i.e. typedef u_xxx_t uxxx_t
+1
source

Thanks to everyone for the answers, I learned something. First of all, about the old naming standard, it seems that the package itself is old and not very supported, on the home page does not seem to work. Looking back at the code, I found 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 

#if was original as follows:

 #if (__sun__ $$ svr4) 

Both macros are defined differently in the system. After the change, it worked fine.

Thanks again!

0
source

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


All Articles