[PATCH] No extern declarations of optarg & co if getopt.h is available
Corinna Vinschen
vinschen at redhat.com
Thu Sep 11 07:10:07 EST 2003
On Wed, Sep 10, 2003 at 03:32:10PM -0500, Ben Lindstrom wrote:
> 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
It's not *my* compiler, it's gcc. And it's not wrong but it's the way
dynamic imorting/exporting works on Windows platforms. What I don't get
is why extern declarations are duplicated here. Either they exist in
getopt.h or they should be declared explicitely in the source but not both.
So I think my patch is correct. The other way is to substitute the
#ifndef HAVE_GETOPT_H
by
#ifndef HAVE_CYGWIN
if you like this better.
Corinna
--
Corinna Vinschen
Cygwin Developer
Red Hat, Inc.
More information about the openssh-unix-dev
mailing list