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

Ben Lindstrom mouring at etoh.eviladmin.org
Thu Sep 11 12:25:21 EST 2003



On Wed, 10 Sep 2003, Corinna Vinschen wrote:

> 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

Track someone down with a native Windows C compiler and setup a proof
test.  Never seen any native compiler have problems with this.  Otherwise
one of my old favorite BBSes (WWIV 4.x) would have been horrible broken
and I know folks that ran WWIV + Multi-line Mods on Windows boxes for the
longest time.

I suspect you have deeper issues with your GNU LD port.

- Ben




More information about the openssh-unix-dev mailing list