[comp.sys.sgi] getopt and C++

markov@cs.ruu.nl (Mark Overmars) (03/05/91)

I have a simple question: How do I use the routine getopt with C++. What I did
was to put

   extern int getopt(int,char **,char *);

in the program. Now the loader complaints that getopt__FiPPcPc is undefined.

How do I solve this?

Mark Overmars

meuer@cs.umn.edu (Mark V. Meuer) (03/05/91)

In article <1991Mar04.161843.27932@cs.ruu.nl>, markov@cs.ruu.nl (Mark Overmars) writes:
> I have a simple question: How do I use the routine getopt with C++. What I did
> was to put
> 
>    extern int getopt(int,char **,char *);
> 
> in the program. Now the loader complaints that getopt__FiPPcPc is undefined.
> 
> How do I solve this?
> 
> Mark Overmars

I *think* all you have to do is change the declaration to

	extern "C" {
		extern int getopt(int,char **,char *);
	} ;

and it should work correctly.   In general, when calling a C function, put
the declarations in a block preceeded by 'extern "C"' and life should be
wonderful.

-mark

ciemo@bananapc.wpd.sgi.com (Dave Ciemiewicz) (03/05/91)

In article <1991Mar04.161843.27932@cs.ruu.nl>, markov@cs.ruu.nl (Mark Overmars) writes:
|> I have a simple question: How do I use the routine getopt with C++. What I did
|> was to put
|> 
|>    extern int getopt(int,char **,char *);
|> 
|> in the program. Now the loader complaints that getopt__FiPPcPc is undefined.
|> 
|> How do I solve this?
|> 
|> Mark Overmars

If you are using C++ 2.0, you should change the extern definition to:

	extern "C" int getopt(int, char**, char*);

The "C" denotes that external routine is a conventional C routine and directs
the compiler to use the non-type-safe (unmangled) name for all references.

-- 
===============================================================================
        __   * __   _  __  ___			
       /  \ / / /  / \/  \/   \		"The sky is falling!"
      /    /  \/  /  /\  /    /		 	said Chicken Little
      \___/\__/\_/  /_/ / \__/		
                       *
===============================================================================