On Windows the C++ function std::system() is essentially a wrapper around the command processor (cmd.exe). When the process finishes, the exit code is passed directly back to you. If your program exits with 1, the integer returned by std::system is 1. On Linux, it might return 256.
On Linux, a single integer return value isn't just an exit code; it's a status word containing a wealth of information about how the process died. The OS packs different data into specific bit ranges. In most Linux implementations, the exit code is shifted into the high byte. This means a return code of 1 is stored as 1 << 8, which equals 256. To get back the exit code, you have to right shift the status code by 8 bits. The portable way is to use WEXITSTATUS macro.
To write code that works on both platforms, you cannot treat the return value as a raw number. You must use the decoding macros provided in <sys/wait.h> on Linux. You should always check if the process actually finished before asking for the code. Here is the safest pattern for Linux:
#include <sys/wait.h>
int status = std::system("./my_script.sh");
if (WIFEXITED(status)) {
int exitCode = WEXITSTATUS(status); //
std::cout << "Success! Code: " << exitCode;
} else if (WIFSIGNALED(status)) {
int sig = WTERMSIG(status);
std::cout << "Killed by signal: " << sig;
}
No comments:
Post a Comment