delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/02/02/16:03:21

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <401EBAAA.505@att.net>
Date: Mon, 02 Feb 2004 16:01:30 -0500
From: David Fritz <zeroxdf AT att DOT net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: libiberty.a and getopt problem
References: <002501c3e982$09877b40$0100a8c0 AT TAMAHOME>
In-Reply-To: <002501c3e982$09877b40$0100a8c0@TAMAHOME>
X-IsSubscribed: yes

The opt* variables are declared __declspec(dllimport) in Cygwin's 
headers. So when you're program refers to optarg after having included 
Cygwin's <getopt.h> (or <unistd.h>), the symbol your program links to is 
called __imp__optarg. Presumably, the opt* variables were not declared 
dllimport when libiberty was compiled. So when you link to libiberty, 
your program and libiberty's getopt() are referring to two different 
optarg variables.

You can work around this problem by not including <getopt.h> or 
<unistd.h>. Though, obviously, this is not a long-term solution.


#include <stdio.h>

extern char *optarg;

int main(int argc, char **argv)
{
     int opt;
     while ((opt = getopt(argc, argv, "avzdrkp:s:o:R:")) != -1) {
         printf("opt = %d,%c\n", opt, opt);
         printf("optarg = %s\n", optarg);
     }
}

-----

~$ gcc test.c
Info: resolving _optarg by linking to __imp__optarg (auto-import)
~$ ./a -s 1
opt = 115,s
optarg = 1
~$ gcc test.c -liberty
~$ ./a -s 1
opt = 115,s
optarg = 1
~$


Cheers


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019