This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: libiberty.a and getopt problem


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]