]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
options: Support use in a shared library on mingw.
authorBruno Haible <bruno@clisp.org>
Sun, 29 Jun 2025 15:45:24 +0000 (17:45 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 29 Jun 2025 15:45:24 +0000 (17:45 +0200)
* lib/options.h (_gl_get_next_option): New declaration.
(get_next_option): On mingw, define in terms of _gl_get_next_option.
* lib/options.c (_gl_get_next_option): On mingw, define this function
instead of get_next_option.

ChangeLog
lib/options.c
lib/options.h

index 21da189bcbd05688dc6b8894abab77f3d18da697..354b34738607b412533e816e0c2aeb1e6b974100 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-06-29  Bruno Haible  <bruno@clisp.org>
+
+       options: Support use in a shared library on mingw.
+       * lib/options.h (_gl_get_next_option): New declaration.
+       (get_next_option): On mingw, define in terms of _gl_get_next_option.
+       * lib/options.c (_gl_get_next_option): On mingw, define this function
+       instead of get_next_option.
+
 2025-06-29  Paul Eggert  <eggert@cs.ucla.edu>
 
        open, openat: port O_DIRECTORY changes to OS X
index f981613bf81cb6b566dd19ae551999f2455aafb9..6ff6064698dfed438bce6c80d88e9e5d35c4fd53 100644 (file)
@@ -121,7 +121,11 @@ _gl_start_options (int argc, char **argv,
 }
 
 int
+#ifdef __MINGW32__
+_gl_get_next_option (int *optind_p, char **optarg_p, int *optopt_p)
+#else
 get_next_option (void)
+#endif
 {
   if (state.argv == NULL)
     {
@@ -141,5 +145,19 @@ get_next_option (void)
               *(options[i].variable) = options[i].value;
           }
     }
+#ifdef __MINGW32__
+  /* On mingw, when this file is compiled into a shared library, it pulls
+     mingw's getopt.o file (that defines getopt_long, opterr, optind, optarg,
+     optopt) into the same shared library.  Since these variables are declared
+     and defined without any __declspec(dllexport) or __declspec(dllimport),
+     the effect is that there are two copies of the variables: one in the
+     shared library and one in the executable.  Upon return from this function,
+     we need to copy the values of the output variables (optind, optarg, optopt)
+     from the shared library into the executable, where the main() function will
+     pick them up.  */
+  *optind_p = optind;
+  *optarg_p = optarg;
+  *optopt_p = optopt;
+#endif
   return ret;
 }
index 044cc73265cd9e3c2ffca1d6078437ed14bce5d9..66d8710ca4f71f58d973d5b0827c036d9512584c 100644 (file)
@@ -247,6 +247,10 @@ extern void _gl_start_options (int argc, /*const*/ char **argv,
    specified) ':'.
    If the processing is terminated, it returns -1.  */
 extern int get_next_option (void);
+#ifdef __MINGW32__
+extern int _gl_get_next_option (int *optind_p, char **optarg_p, int *optopt_p);
+# define get_next_option() _gl_get_next_option (&optind, &optarg, &optopt)
+#endif
 
 #ifdef __cplusplus
 }