__USE_FILE_OFFSET64 vs. _FILE_OFFSET_BITS=64

2019-03-12 03:39发布

问题:

I am trying to maintain code that compiles on lots of different systems. I've seen a dozen different ways of asking for lseek that takes 64-bits. Some systems use lseek64, some use lseeko, some require that you define _FILE_OFFSET_BITS=64, and now I just found a new one that requires that you define __USE_FILE_OFFSET64.

Is there any standard to all of this?

回答1:

There are getconf values in IEEE Std 1003.1-2004 (and a newer set in IEEE Std 1003.1-2008; see also the EXAMPLES section in those documents). Actual compiler options (which might not even be defines) are not specified.

However, the AC_SYS_LARGEFILE macro in autoconf does not try to use this — it tries just -n32 for IRIX, -D_FILE_OFFSET_BITS=64 (which should work for most systems) and -D_LARGE_FILES=1 (apparently for AIX). There is also a reference to Adding Support for Arbitrary File Sizes to the Single UNIX Specification (an older spec draft which was then partially included in the POSIX.1 spec) in autoconf sources.

As for defining __USE_FILE_OFFSET64 manually, not sure if this is really a correct solution — double-underscore macros are reserved for system headers, and most likely there is some conditional definition there which depends on other defines.



回答2:

In features.h, you see the relation between _FILE_OFFSET_BITS and __USE_FILE_OFFSET64.

#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
# define __USE_FILE_OFFSET64    1
#endif

So, only _FILE_OFFSET_BITS is meant for the users.