Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

running getopt( ) on solaris.

Posted on 2004-10-20
13
1,022 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
ID: 12355658
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
ID: 12355678
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
ID: 12355805
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:van_dy
ID: 12356960
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
ID: 12357458
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
ID: 12357908
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12359790
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
ID: 12359825
Also,did you try my first comment?
0
 

Author Comment

by:komlaaa
ID: 12360115
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
ID: 12360446
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
ID: 12360755
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
ID: 12360964
i just send u the files titled 'about getopt from Komlaaa'

komlaaa
0
 
LVL 5

Accepted Solution

by:
van_dy earned 500 total points
ID: 12361575
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

809 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