我应该期望POSIX包含getopt.h吗?

我应该期望POSIX包含getopt.h吗?,posix,getopt-long,Posix,Getopt Long,根据,POSIX库不包括getopt.h。但是,我在unistd.h中发现了这一点: #ifdef __USE_POSIX2 /* Get definitions and prototypes for functions to process the arguments in ARGV (ARGC of them, minus the program name) for options given in OPTS. */ # define __need_getopt # incl

根据,POSIX库不包括
getopt.h
。但是,我在
unistd.h
中发现了这一点:

#ifdef  __USE_POSIX2
/* Get definitions and prototypes for functions to process the
   arguments in ARGV (ARGC of them, minus the program name) for
   options given in OPTS.  */
# define __need_getopt
# include <getopt.h>
#endif
\ifdef\uu使用位置2
/*获取函数的定义和原型以处理
ARGV中的参数(其中的ARGC,减去程序名)
选项中给出的选项*/
#定义uu需要u获取选项
#包括
#恩迪夫

这是否意味着在包含
unistd.h
时,会隐式包含
getopt.h
?我的意思是,上面的代码是我应该从unistd头文件的所有实现中得到的东西,还是只是我特定版本中的东西?另外,是POSIX.2及以后版本中定义的
\u USE\u POSIX2
宏,还是仅用于POSIX.2?

\u USE\u POSIX2
是glibc的一个实现细节;它对应于定义的
\u POSIX\u C\u SOURCE>=2
\u XOPEN\u SOURCE
。这些参数也由
\u GNU\u SOURCE
隐含,除非严格的ANSI模式处于启用状态,否则将隐式使用这些参数。您不应该直接定义u_u使用u宏

因为它对应于一个值
=2
,所以它确实适用于更高版本。有关更多详细信息,请参阅手册页

或者,从features.h中的注释(内部标题-不直接包括-负责所有这些):


getopt(3)
手册页中未提及
getopt.h
。如果您需要
getopt
,您应该包括
unistd.h
,并定义(假设GLIBC)
\u XOPEN\u SOURCE
\u POSIX\u C\u SOURCE=大于\u 2
的内容,而不必担心C库的实现细节。其他环境可能有不同的方式来打开/关闭POSIX功能

请注意,您的标记暗示使用
getopt\u long
。这是GNU扩展,因此不可移植

/* These are defined by the user (or the compiler)
   to specify the desired environment:
...
   _POSIX_C_SOURCE  If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;
            if >=199309L, add IEEE Std 1003.1b-1993;
            if >=199506L, add IEEE Std 1003.1c-1995;
            if >=200112L, all of IEEE 1003.1-2004
            if >=200809L, all of IEEE 1003.1-2008
   _XOPEN_SOURCE    Includes POSIX and XPG things.  Set to 500 if
            Single Unix conformance is wanted, to 600 for the
            sixth revision, to 700 for the seventh revision.