?
Solved

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

Posted on 2004-10-18
9
Medium Priority
?
1,825 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 600 total points
ID: 12345096
Hi komlaaa,
#include <stdlib.h>

Cheers!

Stefan
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

864 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