std::signal does not work.

Steps to reproduce:
  1. In XCode, create a new Command Line Tool Project.

  2. Fill the main.cpp file with this code:

Code Block cpp
#include <csignal>
#include <iostream>
 
namespace
{
volatile std::sig_atomic_t gSignalStatus;
}
 
void signal_handler(int signal)
{
gSignalStatus = signal;
}
 
int main()
{
// Install a signal handler
std::signal(SIGINT, signal_handler);
 
std::cout << "SignalValue: " << gSignalStatus << '\n';
std::cout << "Sending signal " << SIGINT << '\n';
std::raise(SIGINT);
std::cout << "SignalValue: " << gSignalStatus << '\n';
}


Actual output:
SignalValue: 0
Sending signal 2
SignalValue: 0

Expected output:
SignalValue: 0
Sending signal 2
SignalValue: 2

Replies

I know it's 2 years old, but...

If you are using the default Apple "terminal app" the problem is in the terminal and not in the app. By default it is set to declare itself as xterm-256color and that is the problem. Change it to ansi, and the program will behave as expected.

Besides changing the option in terminal preferences (advanced tab), you can do the quick check by examining TERM environment variable from within terminal, i.e.

$ echo $TERM
xterm-256color
$export TERM=ansi
$echo now, your program should behave correctly.
now, your program should behave correctly.