[PATCH] No extern declarations of optarg & co if getopt.h is available

Ben Lindstrom mouring at etoh.eviladmin.org
Thu Sep 11 06:32:10 EST 2003



On Wed, 10 Sep 2003, Corinna Vinschen wrote:

> Hi,
>
> I have a problem with the extern declarations of optarg, optind, etc.
>
> We're currently moving getopt from being a statically linked function
> to a dynamically linked function as part of the Cygwin DLL.  On Windows,
> this requires to generate special symbols (__imp__optarg, etc.), which
> is done by marking the exported variables in the corresponding header.
> Instead of
>
>   extern char *optarg;
>
> getopt.h now contains
>
>   extern char __declspec(dllimport) *optarg;
>
> I'm sorry, but that's not my invention.
>
> Now the problem.  ssh.c, sshd.c and a lot of other files do explicitely
> declare the opt* variables at the beginning of main(), even when a
> getopt.h file is available.  The problem with this is, that this
> explicit extern declaration overrides the declaration from getopt.h.
> So instead of linking against the correct __imp__optarg symbol, the
> linker tries to linked against the normal _optarg.
>
> This could be easily circumvented by either not declaring the variables
> at all in these main() funcs, or by surrounding the declarations with
>
>   #ifndef HAVE_GETOPT_H
>       extern char *optarg;
>       ...
>   #endif

No, I don't think that is right, and in fact it will break older
platforms.

I think your linker/compiler is doing it wrong.  My understand of the C
defintions of 'extern' is it requires that the variable pre-exist (either
fleshed out in headers or in an assocated *.c file or library).

'extern' should never define it's own variable space.

- Ben




More information about the openssh-unix-dev mailing list