• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1131
  • Last Modified:

running getopt( ) on solaris.

the code below runn perfectly on linux but i have trouble running it on solaris 2.8
any suggestion
=================ERROR MESSAGE ======================

main.cpp:42: `optopt' undeclared (first use this function)
main.cpp:42: (Each undeclared identifier is reported only once
main.cpp:42: for each function it appears in.)
main.cpp:50: `optind' undeclared (first use this function)
make: *** [main.o] Error 1

=========================CODE==================
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "wc.h"

int getopt(int, char *const *, const char *);

int main( int argc, char * const argv[])
{
  wc w;

  bool checkL = false;
  bool checkT = false;
  bool checkD = false;
  bool checkC = false;
  bool noArgument = false;

  char * inputFile = NULL;
  int ch;


  char options[] = ":ltdc";

  while ((ch = getopt(argc, argv, options)) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
            break;
    case 't':
      checkT = true;
            break;
    case 'd':
       .
       .
       .
0
komlaaa
Asked:
komlaaa
  • 5
  • 5
  • 3
1 Solution
 
van_dyCommented:
Hello,

Firstoff, do this on ur solaris box.

solaris>  man getopt

it will show you the manpage for getopt.  the man page specifies the header files u will need to
include in your program. once you have included the headers files specified in the man page,
it will work fine. usually it is <unistd.h> but may be its different on ur machine.

hope this helps,
van_dy
0
 
komlaaaAuthor Commented:
More precisely below is the error message

================ERROR MESSAGE ===============
main.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> >::da
ta(void) const':
/afs/clarkson.edu/software/Development/gcc-2.95.2/sun4x_58/lib/gcc-lib/sparc-sun-solaris2.8/2.95.2/../../.
./../include/g++-3/std/bastring.h:152: undefined reference to `getopt(int, char *const *, char const *)'
collect2: ld returned 1 exit status
make: *** [cs450words] Error 1
0
 
ankuratvbCommented:
Remove the declaration of getopt()  from your code:

>>int getopt(int, char *const *, const char *);
Remove this line.

You dont need to do this.its already there in the header files.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
van_dyCommented:
probably getopt() is not present in the system libraries that are being linked
on solaris. you can code it up manually to process the command line switches.
0
 
komlaaaAuthor Commented:
isn't there some standard getopt i can just use as  ' #include "getopt.h" ' instead of manually coding it

thanks,

komlaaa
0
 
van_dyCommented:
Well yea the source code is freely availiable, here i am pasting one from the source tree in my bsd box.
its edited to do away with the library features.

getopt.c-----------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int      opterr = 1,            /* if error message should be printed */
      optind = 1,            /* index into parent argv vector */
      optopt,                  /* character checked for validity */
      optreset;            /* reset getopt */
char      *optarg;            /* argument associated with option */

#define      BADCH      (int)'?'
#define      BADARG      (int)':'
#define      EMSG      ""

/*
 * getopt --
 *      Parse argc/argv argument vector.
 */
int
getopt(nargc, nargv, ostr)
      int nargc;
      char * const *nargv;
      const char *ostr;
{
      static char *place = EMSG;            /* option letter processing */
      char *oli;                        /* option letter list index */

      if (optreset || !*place) {            /* update scanning pointer */
            optreset = 0;
            if (optind >= nargc || *(place = nargv[optind]) != '-') {
                  place = EMSG;
                  return (-1);
            }
            if (place[1] && *++place == '-') {      /* found "--" */
                  ++optind;
                  place = EMSG;
                  return (-1);
            }
      }                              /* option letter okay? */
      if ((optopt = (int)*place++) == (int)':' ||
          !(oli = strchr(ostr, optopt))) {
            /*
             * if the user didn't specify '-' as an option,
             * assume it means -1.
             */
            if (optopt == (int)'-')
                  return (-1);
            if (!*place)
                  ++optind;
            if (opterr && *ostr != ':' && optopt != BADCH)
                  (void)fprintf(stderr, "%s: illegal option -- %c\n",
                      nargv[0], optopt);
            return (BADCH);
      }
      if (*++oli != ':') {                  /* don't need argument */
            optarg = NULL;
            if (!*place)
                  ++optind;
      }
      else {                              /* need an argument */
            if (*place)                  /* no white space */
                  optarg = place;
            else if (nargc <= ++optind) {      /* no arg */
                  place = EMSG;
                  if (*ostr == ':')
                        return (BADARG);
                  if (opterr)
                        (void)fprintf(stderr,
                            "%s: option requires an argument -- %c\n",
                            nargv[0], optopt);
                  return (BADCH);
            }
             else                        /* white space */
                  optarg = nargv[optind];
            place = EMSG;
            ++optind;
      }
      return (optopt);                  /* dump back option letter */
}

-------------------EOF-----------
what you should do is follwoing;
solaris> cc -c getopt.c
this shall produce getopt.o

Now comes the real thing.  getopt.c is a c program and u will need to link it to a c++ program.
You will have linkage issues. Here is how you can resolve them.

in you main.cpp program, remove #include <unistd.h>
put #include <iostream>


after the include directives, u will need to write the following:

extern "C"{ extern int      opterr,            
      optind,            
      optopt,                  
      optreset;            
      extern char      *optarg;           

int getopt(int, char * const *, const char *);
}
using namespace std;
... rest of the main program

now u can compile this with
solaris> c++ main.cpp getopt.o (in ur case g++)

Hope this works fine for you,
van_dy


0
 
ankuratvbCommented:
Hi,

Yes,you can simply do a #include and use the getopt() function without writing any code.
Thats what libraries are for?Aren't they? :~)

In your case,all you need to do is find out which header file contains the getopt() function.

Usually,On Solaris,getopt() is defined under stdlib.h.

And you should use the standard function from the library if its available rather than using its code in your program.
0
 
ankuratvbCommented:
Also,did you try my first comment?
0
 
komlaaaAuthor Commented:
thanks ankuratvb and van dy.

They don't have getopt.h on the solaris i am using but only ' /usr/lib/getopt ' which i believe is the executable.

i am trying Van dy linking procedure. I would let u know when i get it working.

Thanks Experts!!
0
 
komlaaaAuthor Commented:
Van_dy, it is working with the linking method but then it throws a bug into my code. The code gives me the same output for all the file i test on it. If you you can give your email address i can send you my source files(not large) It is a piece of program that is supposed to find the most commonly word in a file. I have a make file run as: make
It produce and executable called cs450words. To run the program just do: ./cs450words fileName.

Thanks,

komlaaa
0
 
van_dyCommented:
ok no problem,
      you may send me your code on revrser@yahoo.co.in  , i will try
and see if it can be made to work this way.
0
 
komlaaaAuthor Commented:
i just send u the files titled 'about getopt from Komlaaa'

komlaaa
0
 
van_dyCommented:
Ok,
     Here is what u need to do

1)   Get the getopt.c file in ur source dir.
2) Make file: Use the following Makefile

----Makefile----

all: cs450words

cs450words:  wc.o main.o getopt.o
      g++ main.o wc.o getopt.o -o cs450words

wc.o: wc.cpp wc.h
      g++ -g -Wall -ansi -c wc.cpp

main.o: main.cpp
      g++ -g -Wall -ansi -c main.cpp

getop.o: getopt.c
      cc -c getopt.c

clean:
      /bin/rm -f cs450words main.o wc.o *~
----------EOF-------------

3) You main.cpp should look like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wc.h"

// int optreset = 1;
      
//      int getopt(int, char *const *, const char *);

extern "C" {
      extern int opterr, optind, optopt, optreset;
      extern char *optarg;

      int getopt(int, char * const *, const char *);
}


int main( int argc, char * const argv[])
{
  wc w;

  bool checkL = false;
  bool checkT = false;
  bool checkD = false;
  bool checkC = false;
  bool noArgument = false;

  char * inputFile = NULL;
  int ch;


  //  char options[] = ":ltdc";

  while ((ch = getopt(argc, argv,":ltdc")) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
            break;
    case 't':
      checkT = true;
            break;
    case 'd':
      checkD = true;
         break;
    case 'c':
      checkC = true;
          break;
    case '?':
    default:
      cout<<"Invalid option specified: "<< optopt<<"\n";
    w.helpMenu();
    exit(1);
    break;
    }

  }

  if(optind == 1){
    noArgument = true;
  }

 

  if(argv[optind])
    inputFile = argv[optind];
  else{
    cout<<"No input file provided\n";
    exit(1);
  }
................/* rest of the main */ -------------

Then it will work fine.

hope this helps,
van_dy



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 5
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now