[comp.lang.c++] signal in C++

chrisdc@mykids.EBay.Sun.COM (chris chang) (04/25/91)

I am a novice in C++.
I am writing a C++ code under SunOS, am having a problem with using
signal.  When I link the program, I get the following undefined 
symbol from the loader: __signal__FiPFv_v.  Any idea?  Also, what is 
the correct syntax for declaring the signal function prototype?

Thank,

Chris
--
Aditya Sehgal  /___   /   /   /\  /   email: sehgala@Ebay.sun.com
CIM Software  ____/  /___/  _/  \/    Engineer, Project Consulting. EB4
------------  ----   ----   -----     ----------------------------------

chrisdc@mykids.EBay.Sun.COM (chris chang) (04/25/91)

I am having a problem with using signal in C++.  I get a undefined 
symbol error message for '_signal__FiPFv_v' when I link the program.
Any ideas?  Also, what's the proper way to declare a function 
prototype for signal?


Thanks,

Chris
--
Aditya Sehgal  /___   /   /   /\  /   email: sehgala@Ebay.sun.com
CIM Software  ____/  /___/  _/  \/    Engineer, Project Consulting. EB4
------------  ----   ----   -----     ----------------------------------

hitz@sim5.csi.uottawa.ca (Martin Hitz) (04/25/91)

In article <6476@male.EBay.Sun.COM> chrisdc@mykids.EBay.Sun.COM (chris chang) writes:
>
>I am having a problem with using signal in C++.  I get a undefined 
>symbol error message for '_signal__FiPFv_v' when I link the program.
>Any ideas?  Also, what's the proper way to declare a function 
>prototype for signal?

The answer to question no. 2 is the answer to question no 1:

extern "C" void (*signal(int sig, void (*func)()))();


To find the answer yourself, follow this procedure:

1) man signal
   This displays the synopsis (for C):
   
	void (*signal(sig, func))()
	void (*func)();

2) Start your protoytpe with

	extern "C"
	
   followed by the main synopsis for signal and a semicolon, yielding
   
   	extern "C" void (*signal(sig, func))();

3) Expand the formal parameter names to parameter prototypes; if no type
   is given, use 'int'. In this example, replace 'func' with its
   declaration. In some complicated cases, this step might cause the
   algorithm to become recursive.
   
However, if you don't care to be type save, you might want to use the
shorthand:

extern "C" void (*signal(...))();

Regards,
	Martin (hitz@csi.UOttawa.CA)

steve@taumet.com (Stephen Clamage) (04/26/91)

chrisdc@mykids.EBay.Sun.COM (chris chang) writes:

>I am writing a C++ code under SunOS, am having a problem with using
>signal.  When I link the program, I get the following undefined 
>symbol from the loader: __signal__FiPFv_v.  Any idea?  Also, what is 
>the correct syntax for declaring the signal function prototype?

Boy, do we need a FAQ.  Any volunteers?  If someone will write it
up and maintain it, I will volunteer to review it.

You need to declare C library functions with
	extern "C"
Otherwise, the C++ compiler assumes they are C++ functions and assumes
a mangled name (type-safe linkage).  If you try to use the C header
files that come with SunOS, they do not have prototypes, but use
old-style C declarations.  These are unusable with C++.  I am told
that Sun C++ comes with its own set of header files with correct
C++ prototypes for all system functions (I have no direct experience
with Sun C++ however).

You don't say which C++ you are using.  If it doesn't supply C++
versions of the system include files, you will have to write your
own, put them in a convenient place, and use the -I directive to
get at them ahead of the standard include files.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

mvm@jedi.harris-atd.com (Matt Mahoney) (04/26/91)

In article <6476@male.EBay.Sun.COM> chrisdc@mykids.EBay.Sun.COM (chris chang) writes:
>
>I am having a problem with using signal in C++.  I get a undefined 
>symbol error message for '_signal__FiPFv_v' when I link the program.
>Any ideas?  Also, what's the proper way to declare a function 
>prototype for signal?
>
Try this:

	extern "C" void (*signal(int sig, void (*handler)(int)))(int);

or more simply

	#include <signal.h>

--------------------------------
Matt Mahoney, mvm@epg.harris.com
#include <disclaimer.h>