我已经下载并安装了jsoncpp库.然后我尝试在我自己的应用程序中使用该库:
#include
void parseJson() {
Json::Reader reader;
}
int main(int argc, char ** argv) {
parseJson();
exit(0);
}
程序编译和链接很好,但在运行时它与SIGSEGV崩溃. gdb回溯看起来像这样:
(gdb) bt
#0 0x0000003a560b7672 in __gnu_cxx::__exchange_and_add () from /usr/lib64/libstdc++.so.6
#1 0x00000000004031e9 in std::string::_Rep::_M_dispose (this=0xffffffffffffffe9, [email protected])
at /usr/lib/gcc/x86_64-redhat-Linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:232
#2 0x0000000000403236 in ~basic_string (this=0x7fffbfe60fb0)
at /usr/lib/gcc/x86_64-redhat-Linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:478
#3 0x00000000004038d4 in ~Reader (this=0x7fffbfe60eb0) at /private/joaho/Parser/opm-parser/external/json/json-cpp/include/json/reader.h:23
#4 0x0000000000402990 in parseJson () at /private/joaho/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:51
#5 0x00000000004029ab in main (argc=1, argv=0x7fffbfe610c8)
at /home/user/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:56
即对我而言,它似乎在析构函数中崩溃了.据我所知,Json :: Reader没有自己的dstructor,所以这必须是默认的析构函数.正如你所看到的,我正在运行一个非常古老的g版本 – 这可能是问题吗?
正如我评论的那样:
When compiled with GCC version 4.8.1 on Debian/Sid (so libjsoncpp-dev 0.6.0~rc2-3) as g++-4.8 -g -Wall -I/usr/include/jsoncpp/ esjson.cc -ljsoncpp -o esjson your program is compiled without warnings, and does not crash when running.
并且GCC 4.1.2非常老(febr. 2007!)并且不再受支持,并且C标准符合性不是很好(GCC,现在版本4.8.1,自4.1以来在C标准一致性方面取得了巨大进步).
所以我不确定GCC 4.1.是错误的,但我不会感到惊讶:它有很差的C声誉,C标准和GCC编译器都得到了很大的改进.升级GCC非常值得,无论是为了更好地支持C还是为了改进诊断和优化.
所以我建议你使用更新的GCC;如果您没有root访问权限,请考虑从源代码tarball编译它;用例如在源树之外构建它../gcc-4.8.1/configure –program-suffix = -4.8 –prefix = $HOME / pub然后make make make install – 在安装了它的依赖项之后