?
Solved

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

Posted on 2004-10-18
9
Medium Priority
?
1,729 Views
Last Modified: 2008-01-09
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);
      .
      .
      .
0
Comment
Question by:komlaaa
[X]
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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12345061
put

#include <getopt.h>

in the begining, the error shall go

hope this heps,
van_dy
0
 

Author Comment

by:komlaaa
ID: 12345076
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
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 600 total points
ID: 12345096
Hi komlaaa,
#include <stdlib.h>

Cheers!

Stefan
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:van_dy
ID: 12345106
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;
}
0
 
LVL 11

Expert Comment

by:avizit
ID: 12345138
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 )
0
 

Author Comment

by:komlaaa
ID: 12345640
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12345650
Did you check if getopt() actually exists in your system's API?

On Solaris 2.8 it's fine.
0
 

Author Comment

by:komlaaa
ID: 12346879
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
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 600 total points
ID: 12353800
Hi,

You dont need the function declaration and the extern variables declaration at the start.
>>
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);
<<

This is already there in unistd.h
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

777 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