Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

undefined reference to `getopt(int, char *const *, char const *)'

Hi Experts World,
I am using getopt to parse command line argument in C++.
How i am using it is the code below. can anyone tell me why i am getting the error message:
=======ERROR MESSAGE =========
undefined reference to `getopt(int, char *const *, char const *)'
collect2: ld returned 1 exit status
make: *** [wc.out] Error 1

======== CODE ========
#include "wc.h"
#include <stdio.h>
#include <unistd.h>


extern char *optarg;
extern int optind ;
extern int optopt;
extern int opterr;
extern int optreset ;


int getopt(int argc, char * const argv[], const char *optstring);


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

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

  char * inputFile = NULL;
  int ch;

  while ((ch = getopt(argc, argv, ":l:t:d:c:")) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
      inputFile = optarg;
      break;
    case 't':
      checkT = true;
      inputFile = optarg;
      break;
    case 'd':
     checkD = true;
      inputFile = optarg;
    case 'c':
      checkC = true;
      inputFile = optarg;
    case ':':
            if(optarg = NULL){
            cout<<"file Name missing\n";
            exit(1);
            }
            else noArgument = true;
    case '?':
    default:
      cout<<"Invalid option specified: \n"<< optopt<<"\n";
    w.helpMenu();
    }
  }
  argc -= optind;
  argv += optind;

  ifstream f(inputFile);
      .
      .
      .
Avatar of van_dy
van_dy

put

#include <getopt.h>

in the begining, the error shall go

hope this heps,
van_dy
Avatar of komlaaa

ASKER

I tried that it give the error message below.
Note: i am using C++

> make
g++ -g -Wall -ansi -c main.cpp wc.cpp
main.cpp:4: getopt.h: No such file or directory
make: *** [main.o] Error 1
SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well i just compiled this piece of code and it seems to compile nicely:

main.cpp
-------------

#include <iostream>
#include <unistd.h>


extern char *optarg;
extern int optind ;
extern int optopt;
extern int opterr;
extern int optreset ;

int getopt(int argc, char * const argv[], const char *optstring);

int main( int argc, char *argv[])
{

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

  char * inputFile = NULL;
  int ch;

  while ((ch = getopt(argc, argv, ":l:t:d:c:")) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
      inputFile = optarg;
      break;
    case 't':
      checkT = true;
      inputFile = optarg;
      break;
    case 'd':
     checkD = true;
      inputFile = optarg;
    case 'c':
      checkC = true;
      inputFile = optarg;
    case ':':
            if(optarg = NULL){
            std::cout<<"file Name missing\n";
            exit(1);
            }
            else noArgument = true;
    case '?':
    default:
      std::cout<<"Invalid option specified: \n"<< optopt<<"\n";
    }
  }

  std::cout << checkL << ' ' << checkT << ' ' << checkD << ' ' << checkC << '\n';
  return 0;
}
can you compile the program given in

http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html

if that doesn't compile I guess there is something wrong with your system headers etc ( assuming you are on a GNU system that is )
Avatar of komlaaa

ASKER

Thanks, but sorry i did not mention it is on UNIX using g++ compiler.
i tried the code above and it is giving me the error message:

crux:~/achille/temp> g++ myGetopt.cpp
/var/tmp/ccGa6epa.o: In function `main':
/var/tmp/ccGa6epa.o(.text+0x120): undefined reference to `getopt(int, char *const *, char const *)'
collect2: ld returned 1 exit status


Thanks.
Did you check if getopt() actually exists in your system's API?

On Solaris 2.8 it's fine.
Avatar of komlaaa

ASKER

1.) i did include  #include <stdlib.h> but still.
2.) i did not check my system(UNIX) to see if it has getopt( ),
but the question is if not what should i do. Since i have to run this program on
this system.

Please help Experts!!!

Thanks,

komlaaa
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial