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

发布于 2024-10-08 11:08:44 字数 996 浏览 0 评论 0原文

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

__REDIRECT_NTH 是什么意思?

GNU unistd.h has this bit of magic:

/* 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

What does __REDIRECT_NTH mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

游魂 2024-10-15 11:08:44

有关 REDIRECT_NTH 含义的更多详细信息:该宏生成一个函数声明,告诉编译器在编译器的 ELF 输出中为该函数使用特定符号。默认情况下,编译器使用 ELF 符号“lseek”来表示名为“lseek”的 C 函数(或者在某些系统上为“_lseek” ”)。该宏扩展为告诉编译器使用符号“lseek64”的代码。因此,C 代码有一个名为“lseek”的函数,但是当您查看目标代码时(例如,使用程序“nm”),您会看到“lseek64”。

其目的是该函数实际上是二进制级别的 lseek64 - 它处理 64 位文件偏移量。但出于向后源兼容性的原因(这就是 _FILE_OFFSET_BITS=64 的意思),源代码已声明将其称为 lseek

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

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

More detail on what REDIRECT_NTH means: The macro generates a function declaration that tells the compiler to use a specific symbol for the function in the compiler's ELF output. By default, the compiler uses the ELF symbol "lseek" for a C function named "lseek" (or, on some systems, "_lseek"). This macro expands to code that tells the compiler to use the symbol "lseek64" instead. So the C code has a function named "lseek", but when you look in the object code (e.g. with program 'nm'), you see "lseek64".

The purpose of that is that the function really is lseek64 at a binary level - it deals in 64 bit file offsets. But the source code has declared that it wants to call it lseek, for backward source compatibility reasons (that's what _FILE_OFFSET_BITS=64 says).

If the source program wants to call lseek64 by that name, and have lseek refer to the old 32 bit version, it must define _LARGEFILE64_SOURCE and not _FILE_OFFSET_BITS=64.

By the way, the "NTH" in "REDIRECT_NTH" refers to "no throw," which is an attribute of the function declaration the macro generates.

妞丶爷亲个 2024-10-15 11:08:44

它是命名空间中保留给实现的宏。它仅出现在您的平台上;它会执行适合您平台的任何操作,因为实现决定它是正确的。

如果您无法轻松找到,您可能不应该直接使用它。即使您能找到答案,您也很可能不应该直接使用它。

也就是说,它似乎安排对 lseek() 的调用转换为 lseek64(),大概是为了提供对大文件(大于 2 GiB)的支持在 32 位计算机上(或在 64 位计算机上的 32 位系统 API 下)。

It is a macro in the namespace reserved to the implementation. It only appears on your platform; it does whatever is appropriate on your platform because the implementation decided it is correct.

If you can't find out easily, you probably shouldn't be using it directly. Even if you can find out, there's a good chance you should not be using it directly.

That said, it looks as though it arranges for calls to lseek() to be translated to lseek64(), presumably to provide support for large files (bigger than 2 GiB) on 32-bit machines (or under the 32-bit system API on your 64-bit machine).

稀香 2024-10-15 11:08:44

永远不要自己接触大文件黑客。始终在 CFLAGS 中包含 -D_FILE_OFFSET_BITS=64,这样您就永远不会出错。

Never touch the largefile hacks yourself. Always include -D_FILE_OFFSET_BITS=64 in your CFLAGS and you can never go wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文