2016-06-13 84 views
0

我想使用Jsoncpp創建Json消息。 我做了如下:使用JsonCpp創建Json消息

g++ -o clients clients.cpp -ljsoncpp -lzmq 

這個誤差發生種類:

clients.cpp:(.text._ZN20multi_usrp_emulation7client1Ev[_ZN20multi_usrp_emulation7client1Ev ]+0x1d9): undefined reference to `Json::Value::operator=(Json::Value)' 
clients.cpp:(.text._ZN20multi_usrp_emulation7client1Ev[_ZN20multi_usrp_emulation7client1Ev ]+0x224): undefined reference to `Json::Value::operator=(Json::Value)' 
clients.cpp:(.text._ZN20multi_usrp_emulation7client1Ev[_ZN20multi_usrp_emulation7client1Ev ]+0x26c): undefined reference to `Json::Value::operator=(Json::Value)' 
collect2: error: ld returned 1 exit status 

什麼是錯我的代碼

#include <string> 
#include <iostream> 
#include <sstream>  
#include <json/json.h> 

int main() 
{ 
    std::string Value = "5.17e9"; 
    std::string Type = "TX"; 
    std::string Parameter = "Frequency"; 

    Json::Value root; 
    root.append("Type"); 
    root.append("Parameter"); 
    root.append("Value"); 
    root["Type"] = Type; 
    root["Parameter"] = Parameter; 
    root["Value"] = Value; 

    Json::FastWriter fastwriter; 
    std::string message = fastwriter.write(root); 
    std::cout<<message<<std::endl; 

    return 0; 
} 

使用下面的命令行編譯此代碼?

+0

什麼版本jsoncpp您使用的是?也許它與這個問題有關:https://github.com/open-source-parsers/jsoncpp/issues/484? –

+0

我不怎麼檢查版本。但我今天通過github下載了。我讀了你附帶的問題,但我不明白它的意思..你能幫助我嗎? –

+0

版本是1.7.2的 –

回答

0

我不確定鏈接錯誤,但是在編譯器中可能以不同方式處理的代碼存在問題。這對我來說是一個運行時錯誤。

Json::Value root; 
root.append("Type"); // makes root into arrayValue 
root["Type"] = Type; // accesses root as an objectValue 
// triggers assert in Json::Value::resolveReference 

這是我要做的事:

Json::Value root; 
root["Type"] = Type; 
+0

太棒了!它的作品感謝人! –