0

我正在查看代码ls.c并注意到对xstrtoul(). 我想知道这个有用的函数到底是在哪里定义的。这导致了我xstrtol.h,它有以下片段:

# define _DECLARE_XSTRTOL(name, type) \
  strtol_error name (const char *, char **, int, type *, const char *);
_DECLARE_XSTRTOL (xstrtol, long int)
_DECLARE_XSTRTOL (xstrtoul, unsigned long int)
_DECLARE_XSTRTOL (xstrtoimax, intmax_t)
_DECLARE_XSTRTOL (xstrtoumax, uintmax_t)

如果我理解正确,预处理器通过后生成的函数原型将是:

strtol_error xstrtoul (const char *, char **, int, unsigned long int *, \
                                                                   const char *);

xstrtol.c然而,在is中定义的唯一相关函数__xstrtol()具有以下签名:

strtol_error
__xstrtol (const char *s, char **ptr, int strtol_base,
           __strtol_t *val, const char *valid_suffixes)

我的猜测是,不知何故,编译器每次都映射这个函数的多个实例,用另一个名称代替__xstrtol,另一种类型代替__strtol_t. 但我不明白这是在哪里/如何完成的。#define(在顶部的这两个中只有一个xstrtol.c)。

4

1 回答 1

2

好的,该xstrtol.c文件是真正定义函数的地方,但是该源文件包含在其他源文件中,就像 C++ 函数模板一样,以生成名称和行为略有不同的函数。

看看xstrtoul.c。它实际上包括xstrtol.c但定义了几个预处理器符号来修改模板函数生成:

#define __strtol strtoul
#define __strtol_t unsigned long int
#define __xstrtol xstrtoul
#define STRTOL_T_MINIMUM 0
#define STRTOL_T_MAXIMUM ULONG_MAX
#include "xstrtol.c"

.c将一个文件包含到另一个文件中是不寻常的(但并非闻所未闻).c

在 C++ 中,有一些文件命名约定用于类似的情况,即模板定义.tcc头文件#included末尾的文件中定义。.hpp

于 2013-06-25T23:11:03.560 回答