Solved

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

Posted on 2004-10-18
9
1,589 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
  • 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 200 total points
ID: 12345096
Hi komlaaa,
#include <stdlib.h>

Cheers!

Stefan
0
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 200 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
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 recursion in the C programming language.
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.

929 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

10 Experts available now in Live!

Get 1:1 Help Now