My case is pretty simple: I want my C++ program to deal with Unix signals. To do so, glibc provides a function in signal.h called sigaction
, which expects to receive a function pointer as its second argument.
extern "C"
{
void uponSignal(int);
}
void uponSignal(int)
{
// set some flag to quit the program
}
static
void installSignalHandler()
{
// initialize the signal handler
static struct sigaction sighandler;
memset( &sighandler, 0, sizeof(struct sigaction) );
sighandler.sa_handler = uponSignal;
// install it
sigaction( SIGINT, &sighandler, nullptr );
}
My question is: is the extern "C"
linkage specifier necessary?
Bonus question: can uponSignal be declared static
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…