我可以在 mac 上使用 C++ 获得 -1 退出代码吗?

Can I get a -1 exit code using C++ on mac?

我只是想知道是否可以在 Unix 系统上将 C++ 中的函数设为 return -1 而不是 255?

我一直在编写一些 OpenCV 的东西并意识到这些东西没有按预期工作。我使用的函数本来是 return -1,但一直 returning 255。所以我只是想知道是否可以进行任何配置以允许 return 代码为 -1?

这是我在课本中使用的示例

int main( int argc, char** argv ) {
  int i = 0;
  cv::namedWindow( "Example3", cv::WINDOW_AUTOSIZE );
  cv::VideoCapture cap;
  cap.open( std::string(argv[1]) );
  cv::Mat frame;
  for(;;) {
    cap >> frame;
    if( frame.empty() ) break; // Ran out of film
    cv::imshow( "Example3", frame );
    std::cout<<"waitkey:"<<cv::waitKey(33) << std::endl;
  }
  return -1;
}

可以找到关于waitkey()的信息here

POSIX says,

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available from wait() and waitpid(); the full value shall be available from waitid() and in the siginfo_t passed to a signal handler for SIGCHLD.

所以,是 shell 程序在使用某些 POSIX API 来管理其子项时将值缩小到 8 位。

看来这个问题实际上是OpenCV代码的一个bug。该错误已被解决并推送到 OpenCV github

的 master 分支