Solved

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

Posted on 2004-10-18
9
1,650 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
Independent Software Vendors: 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!

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

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

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 297
Test against App 49 138
How to creat good software interface ? 1 71
Raspberry Pi 3 to send text message 9 195
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

733 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