jsoncpp,c++语言中解析json最常用的库了,以前都是在项目中直接用。但是今天突然想单拿出来使用下,怎么编译都过不了,报错内容如下:
[root@node1 windows_share]# g++ jsoncpp_test.cc /tmp/ccAclhTh.o: In function `main': jsoncpp_test.cc:(.text+0x51): undefined reference to `Json::Reader::Reader()' jsoncpp_test.cc:(.text+0x62): undefined reference to `Json::Value::Value(Json::ValueType)' jsoncpp_test.cc:(.text+0x7e): undefined reference to `Json::Reader::parse(std::string const&, Json::Value&, bool)' jsoncpp_test.cc:(.text+0xb2): undefined reference to `Json::Value::operator[](char const*)' jsoncpp_test.cc:(.text+0xba): undefined reference to `Json::Value::asInt() const' jsoncpp_test.cc:(.text+0xce): undefined reference to `Json::Value::operator[](char const*)' jsoncpp_test.cc:(.text+0xe0): undefined reference to `Json::Value::asString() const' jsoncpp_test.cc:(.text+0x151): undefined reference to `Json::Value::~Value()' jsoncpp_test.cc:(.text+0x1aa): undefined reference to `Json::Value::~Value()' collect2: error: ld returned 1 exit status整个错误很明显,就是少了jsoncpp相关的库。但是查看了项目的CMakeLists.txt,发现也是没有引用相关库的。谷歌百度了一番,都是要安装库,最后发现还是太麻烦了,于是我依照平时写的项目中的方式直接编译源码方式来搞。新建了一个项目如下:
[root@node1 jsoncpptest]# ll total 12 -rw-r--r--. 1 root root 416 May 3 12:06 CMakeLists.txt drwxr-xr-x. 3 root root 4096 May 3 11:16 jsoncpp-0.5.0 -rw-r--r--. 1 root root 555 May 3 11:46 jsoncpp_test.ccjsoncpp-0.5.0中的文件如下:
root@node1 jsoncpptest]# cd jsoncpp-0.5.0/ [root@node1 jsoncpp-0.5.0]# ll total 64528 -rw-r--r--. 1 root root 438 May 3 11:16 autolink.h -rw-r--r--. 1 root root 1827312 May 3 11:16 autolink.h.gch -rw-r--r--. 1 root root 1538 May 3 11:16 config.h -rw-r--r--. 1 root root 1827248 May 3 11:16 config.h.gch -rw-r--r--. 1 root root 1290 May 3 11:16 features.h -rw-r--r--. 1 root root 1864176 May 3 11:16 features.h.gch -rw-r--r--. 1 root root 735 May 3 11:16 forwards.h -rw-r--r--. 1 root root 1839568 May 3 11:16 forwards.h.gch -rw-r--r--. 1 root root 3929 May 3 11:16 json_batchallocator.h -rw-r--r--. 1 root root 2422032 May 3 11:16 json_batchallocator.h.gch -rw-r--r--. 1 root root 200 May 3 11:16 json.h -rw-r--r--. 1 root root 15847440 May 3 11:16 json.h.gch -rw-r--r--. 1 root root 12669 May 3 11:16 json_internalarray.inl -rw-r--r--. 1 root root 16292 May 3 11:16 json_internalmap.inl -rw-r--r--. 1 root root 21108 May 3 11:16 json_reader.cpp -rw-r--r--. 1 root root 40024 May 3 11:16 json_value.cpp -rw-r--r--. 1 root root 7367 May 3 11:16 json_valueiterator.inl -rw-r--r--. 1 root root 20450 May 3 11:16 json_writer.cpp -rw-r--r--. 1 root root 6486 May 3 11:16 reader.h -rw-r--r--. 1 root root 15474608 May 3 11:16 reader.h.gch -rw-r--r--. 1 root root 34010 May 3 11:16 value.h -rw-r--r--. 1 root root 10320976 May 3 11:16 value.h.gch -rw-r--r--. 1 root root 6188 May 3 11:16 writer.h -rw-r--r--. 1 root root 14425840 May 3 11:16 writer.h.gch编写CMakeLists.txt,如下:
cmake_minimum_required(VERSION 2.6) project (JSONCPP_TEST) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -g -Wall -O0 -Wno-unused-variable") link_directories( ${PROJECT_SOURCE_DIR}/lib ) set(maintest jsoncpp_test.cc ) set(json_srcs jsoncpp-0.5.0/json_reader.cpp jsoncpp-0.5.0/json_value.cpp jsoncpp-0.5.0/json_writer.cpp ) add_executable(jsoncpptest ${maintest} ${json_srcs}) TARGET_LINK_LIBRARIES(jsoncpptest)编译结果:
[root@node1 jsoncpptest]# cmake . -- The C compiler identification is GNU 4.8.5 -- The CXX compiler identification is GNU 4.8.5 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Configuring done -- Generating done -- Build files have been written to: /root/windows_share/jsoncpptest [root@node1 jsoncpptest]# make Scanning dependencies of target jsoncpptest [ 25%] Building CXX object CMakeFiles/jsoncpptest.dir/jsoncpp_test.cc.o [ 50%] Building CXX object CMakeFiles/jsoncpptest.dir/jsoncpp-0.5.0/json_reader.cpp.o [ 75%] Building CXX object CMakeFiles/jsoncpptest.dir/jsoncpp-0.5.0/json_value.cpp.o [100%] Building CXX object CMakeFiles/jsoncpptest.dir/jsoncpp-0.5.0/json_writer.cpp.o Linking CXX executable jsoncpptest [100%] Built target jsoncpptest [root@node1 jsoncpptest]# ll total 868 -rw-r--r--. 1 root root 12061 May 3 12:09 CMakeCache.txt drwxr-xr-x. 5 root root 4096 May 3 12:09 CMakeFiles -rw-r--r--. 1 root root 1603 May 3 12:09 cmake_install.cmake -rw-r--r--. 1 root root 430 May 3 12:09 CMakeLists.txt drwxr-xr-x. 3 root root 4096 May 3 11:16 jsoncpp-0.5.0 -rwxr-xr-x. 1 root root 844456 May 3 12:09 jsoncpptest -rw-r--r--. 1 root root 555 May 3 11:46 jsoncpp_test.cc -rw-r--r--. 1 root root 8165 May 3 12:09 Makefile [root@node1 jsoncpptest]# ./jsoncpp -bash: ./jsoncpp: No such file or directory [root@node1 jsoncpptest]# ./jsoncpptest code:2 msg:not login, please login first!
最后源码如下:
#include <iostream> #include <string> #include "./jsoncpp-0.5.0/json.h" int main(int argc, char* argv[]) { std::string data = "{\"code\": 2, \"msg\": \"not login, please login first!\"}"; Json::Reader JsonReader; Json::Value JsonRoot; if (!JsonReader.parse(data, JsonRoot)) { std::cout << "json parse fail" << std::endl; } int code = JsonRoot["code"].asInt(); std::string msg = JsonRoot["msg"].asString(); std::cout << "code:" << code << std::endl; std::cout << "msg:" << msg << std::endl; return 0; }
