#define __REDIRECT_NTH在unistd.h中做了什么?

vy3*_*y32 3 c unistd.h

GNU unistd.h有一点魔力:

/* Move FD's file position to OFFSET bytes from the
   beginning of the file (if WHENCE is SEEK_SET),
   the current position (if WHENCE is SEEK_CUR),
   or the end of the file (if WHENCE is SEEK_END).
   Return the new file position.  */
#ifndef __USE_FILE_OFFSET64
extern __off_t lseek (int __fd, __off_t __offset, int __whence) __THROW;                                                                    
#else
# ifdef __REDIRECT_NTH
extern __off64_t __REDIRECT_NTH (lseek,
                              (int __fd, __off64_t __offset, int __whence),
                               lseek64);                                                                                                    
# else
#  define lseek lseek64
# endif
#endif
#ifdef __USE_LARGEFILE64
extern __off64_t lseek64 (int __fd, __off64_t __offset, int __whence) __THROW;                                                              
#endif
Run Code Online (Sandbox Code Playgroud)

__REDIRECT_NTH是什么意思?

gir*_*ata 5

更多详细信息REDIRECT_NTH:宏生成一个函数声明,告诉编译器在编译器的ELF输出中使用特定符号作为函数.默认情况下,编译器将ELF符号" lseek"用于名为" lseek" 的C函数(或者,在某些系统上使用" _lseek").此宏扩展为代码,告诉编译器使用符号"lseek64".所以C代码有一个名为" lseek" 的函数,但是当你查看目标代码时(例如用程序'nm'),你会看到" lseek64".

这样做的目的是该函数在二进制级别确实是lseek64 - 它处理64位文件偏移.但是源代码已声明它想要调用它lseek,因为后向源兼容性原因(这_FILE_OFFSET_BITS=64就是说).

如果源程序想要lseek64通过该名称调用,并且lseek引用旧的32位版本,则必须定义_LARGEFILE64_SOURCE而不是_FILE_OFFSET_BITS=64.

顺便说一句,""中的"NTH" REDIRECT_NTH指的是"no throw",它是宏生成的函数声明的属性.