This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 31b10d3055d61e8e64abea5d0e009bad03736e59 Author: Marco Casaroli <[email protected]> AuthorDate: Tue Oct 20 17:20:01 2026 +0200 include/unistd.h: Declare the getopt() variables with NUTTX_NO_GETOPT_MACROS. POSIX requires <unistd.h> to declare optarg, opterr, optind and optopt as extern variables: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/unistd.h.html NuttX keeps these values per task, so <unistd.h> defines the names as macros. PR #20568 kept the macros as the default, the same in flat, protected and kernel builds, and added NUTTX_NO_GETOPT_MACROS for a program that brings its own getopt() and its own variables. With it, <unistd.h> does not define the macros, but it does not declare the names either. A file that includes only <unistd.h> and reads optind, while another file or library defines it, does not compile. Declare the four names as the extern variables that POSIX specifies when NUTTX_NO_GETOPT_MACROS is defined, in every build mode. libc still defines no variables of these names. A program that defines NUTTX_NO_GETOPT_MACROS but does not bring its own getopt() and variables still fails to build: the link finds no optarg, opterr, optind or optopt. Without the define nothing changes. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- include/unistd.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/unistd.h b/include/unistd.h index 66cddf68d17..4a02ba06479 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -290,7 +290,9 @@ /* Accessor functions associated with getopt(). getopt() keeps optarg, * opterr, optind and optopt per task, so the names are macros. A program * that brings its own getopt() and its own variables of these names can - * define NUTTX_NO_GETOPT_MACROS to keep the names free. + * define NUTTX_NO_GETOPT_MACROS: then the names are declared as the + * variables that POSIX specifies (see below), and the program or another + * library defines them. */ #ifndef NUTTX_NO_GETOPT_MACROS @@ -497,6 +499,13 @@ FAR int *getopterrp(void); /* Print error message */ FAR int *getoptindp(void); /* Index into argv */ FAR int *getoptoptp(void); /* Unrecognized option character */ +#ifdef NUTTX_NO_GETOPT_MACROS +EXTERN FAR char *optarg; /* Optional argument following option */ +EXTERN int opterr; /* Print error message */ +EXTERN int optind; /* Index into argv */ +EXTERN int optopt; /* Unrecognized option character */ +#endif + int gethostname(FAR char *name, size_t namelen); int sethostname(FAR const char *name, size_t namelen);
