c++ - 用iibjson试试,catch不行,在target Cross compiled linux board

标签 c++ linux libjson

我在 C++ 类的构造函数中有一个 try catch

class jsonAdap
{
  jsonAdap(const char *text)
 {
   try
   {
   parsejson(text);//this calls a libjson function that throws the expection throw std::invalid_argument("exception")
   }
   catch(std::invalid_argument)
   {
     cout<<"Exception captured:"<<endl"
    }

 }
}

当我创建此类的对象时,它给出错误并在抛出“std::invalid_argument”实例后停止终止调用。

这是在 ARM 上交叉编译的情况(使用 -O0 -g3 -Wall -c -fmessage-length=0 -pthread -Wno-reorder -std=gnu++0x -fstack-protector-all -Wno- format-contains-nul -Wno-format-extra-args -Wno-format-zero-length) ,

但在 Windows 上它会正确捕获错误。

当我尝试使用来自交叉编译的相同选项的示例应用程序时,它在板上运行完美。

是否有任何我可能没有注意到的可能导致这种行为的编译器设置?

有什么建议吗?

下面是示例应用程序

class ctest
{
public:
ctest()
{

    int x = -1;
   try {
      cout << "Inside try \n";
      if (x < 0)
      {
        throw std::invalid_argument("test");
         cout << "After throw (Never executed) \n";
      }
   }
   catch (std::invalid_argument &e)  {
      cout << "Exception Caught \n";
   }
}


void test(){};
};

int main( int argc, char* argv[] )
{
  cout << "Before try \n";
  ctest c;
  cout << "cdone "<<endl;
  return 0;
}

最佳答案

问题的根源似乎在libjson:JSONStream,它有重载的<< & parse函数确实会抛出异常,但在他们的签名中他们只是throw(),表明它不会抛出异常.

因此,当实际发生异常时,将按照此处的说明调用终止 http://www.gotw.ca/publications/mill22.htm

解决方案是更改 libjson JSONStream 类函数(<< & parse)的签名 - 在 libjson 中提出错误单以修改 JSONStream 类函数(<< & parse)的签名

终于成功了。

Windows 编译器似乎忽略了这一点,但 linux g++ 编译器没有

关于c++ - 用iibjson试试,catch不行,在target Cross compiled linux board,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981882/

相关文章:

linux - 在 Linux 上安装 libjson-rpc-perl

c++ - JSONNODE 未在此范围内声明

c++ - 在 noexcept 函数体中处理 std::optional 是否安全?

c++ - 显示私有(private)数组变量 C++

linux - 如何在 Debian Linux 中检查驱动器是否存在

linux - linux 上的 matlab 无法绘制任何东西(无法加载 libstdc++.so.6 : version `CXXABI_1.3.8' not found)

c++ - 使用 libJson 创建结构数组

C++ UTF-8 字符串

c++ - 运算符重载段错误

linux - 查找命令表达式错误