I was trying to use the JSONCPP library to work with JSON documents in C++. Reading another question here, I saw this example and tried to implement:
#include "json/json.h"
int main(){
Json::Value event;
Json::Value vec(Json::arrayValue);
vec.append(Json::Value(1));
vec.append(Json::Value(2));
vec.append(Json::Value(3));
event["competitors"]["home"]["name"] = "Liverpool";
event["competitors"]["away"]["code"] = 89223;
event["competitors"]["away"]["name"] = "Aston Villa";
event["competitors"]["away"]["code"] = vec;
std::cout << event << std::endl;
}
But I keep getting this error:
json2.cpp:(.text+0x1c): referência não definida para "Json::Value::Value(Json::ValueType)"
/usr/bin/ld: json2.cpp:(.text+0x30): referência não definida para "Json::Value::Value(Json::ValueType)"
json2.cpp is the file's name, as it looks like. Being a /usr/bin/ld a link error, I tried to add as parameter the directory path to the compile command but the error persists
Does anyone know how to fix it?
It’s all about how you build/install the library and specify the headers in the source. Since you didn’t mention what method you took to build the library, I’ll demonstrate three solutions I got to work on Ubuntu 20.04 LTS.
Install the Package
Most straightforward way is to install via apt:
The header files will be installed to /usr /include/jsoncpp/json and can be included in json2.cpp as:
And compile by adding the linker flag as:
Amalgamated Source
Inside of the top-level directory of the jsoncpp repository, you can amalgamate the source by:
By default, this will generate all necessary source and header files in the dist/ directory, and there is no need to link the library. Include in json2.cpp as:
As indicated in the build instructions, jsoncpp.cpp will have to be incorporated into your project:
Cmake
From the top-level directory of the jsoncpp repository:
This will install the header files to /usr/local/include/json. Include in json2.cpp as:
And compile by adding the linker flag as: