Solved

running getopt( ) on solaris.

Posted on 2004-10-20
13
998 Views
Last Modified: 2013-12-05
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
Comment
Question by:komlaaa
  • 5
  • 5
  • 3
13 Comments
 
LVL 5

Expert Comment

by:van_dy
Comment Utility
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
 

Author Comment

by:komlaaa
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 
LVL 5

Expert Comment

by:van_dy
Comment Utility
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
 

Author Comment

by:komlaaa
Comment Utility
isn't there some standard getopt i can just use as  ' #include "getopt.h" ' instead of manually coding it

thanks,

komlaaa
0
 
LVL 5

Expert Comment

by:van_dy
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
Also,did you try my first comment?
0
 

Author Comment

by:komlaaa
Comment Utility
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
 

Author Comment

by:komlaaa
Comment Utility
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
 
LVL 5

Expert Comment

by:van_dy
Comment Utility
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
 

Author Comment

by:komlaaa
Comment Utility
i just send u the files titled 'about getopt from Komlaaa'

komlaaa
0
 
LVL 5

Accepted Solution

by:
van_dy earned 500 total points
Comment Utility
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now