C++ writing JSON with JSONCPP - c++

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:
$ sudo apt-get install libjsoncpp-dev
The header files will be installed to /usr /include/jsoncpp/json and can be included in json2.cpp as:
#include <jsoncpp/json/json.h>
And compile by adding the linker flag as:
$ g++ json2.cpp -o json2 -ljsoncpp
Amalgamated Source
Inside of the top-level directory of the jsoncpp repository, you can amalgamate the source by:
$ python amalgamate.py
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:
#include <json/json.h>
As indicated in the build instructions, jsoncpp.cpp will have to be incorporated into your project:
$ g++ json2.cpp dist/jsoncpp.cpp -Idist -o json2
Cmake
From the top-level directory of the jsoncpp repository:
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ..
$ make; make install
This will install the header files to /usr/local/include/json. Include in json2.cpp as:
#include <json/json.h>
And compile by adding the linker flag as:
$ g++ json2.cpp -o json2 -ljsoncpp

Related

Why in my Linux, when current work directory is owned by root (e.g /usr/bin), then I cannot link any library while compiling c++?

If I write this code and saved as a.cpp at ~/Desktop
#include <memory>
int main(){}
then input to bash:
cd /usr/bin
g++ -g ~/Desktop/a.cpp -o ~/Desktop/a
then the g++ will output plenty of messy code of errors.
I have found the reason is because it don't have authority to link XX.so library.
But if I add a 'sudo' , or set CWD to the path owned by user, g++ will work properly, as follows:
sudo g++ -g ~/Desktop/a.cpp -o ~/Desktop/a
or
cd ~/Desktop
g++ -g ~/Desktop/a.cpp -o ~/Desktop/a
Why do this happen? or how can I fix this?
You don't want to generate code directly in /usr/bin.
You generate your code in your user folder, maybe create a sub-directory called cppwork or something like that.
cd
mkdir cppwork
cd cppwork
g++ -g a.cpp -o a
Once you compiled in your directory, then you copy the file using install which will also take care of stripping the debug if any (i.e. the -g says to keep debug info—stripping is not mandatory).
sudo install -s a /usr/bin/a
As you can see, the place where I use sudo is with the install command.
That being said, I never use those directly. Now a day, I use cmake which means everything works automatically. But that would be a different discussion.
Thanks for every one. I have found the reason. It's because there is an executable program named 'array' in /usr/bin. And when CWD is /usr/bin, the compiler regard this 'array' as the c++ header <array>, so compiling error.
Then I need to find out why the compiler includes /usr/bin by mistake.

Tensorflow c++ api

I recently tensorflow cpp api from a way below(on MacOS Catalina ):
1. I first Install Bazel 0.23.0
2. download tensorflow:
$ git clone https://github.com/tensorflow/tensorflow
cd tensorflow # cd to the top-level directory created
$ ./configure
bazel build //tensorflow:libtensorflow_cc.so
# build C library
$ bazel build //tensorflow:libtensorflow.so
After install I run the code below:
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/env.h"
int main(){
return 0;
}
with. command below:
g++ -I/usr/local/Cellar/libtensorflow/2.1.0_1/include -L/usr/local/Cellar/libtensorflow/2.1.0_1/lib -ltensorflow -ltensorflow_cc -ltensorflow_framework -std=c++11 main.cc -o main.o
but I get Error below:
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:17:2: error:
This file was generated by an older version of protoc which is
#error This file was generated by an older version of protoc which is
^
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:18:2: error:
incompatible with your Protocol Buffer headers. Please
#error incompatible with your Protocol Buffer headers. Please
I used libprotoc 3.11.4.
what should I do? what is wrong?

Installing and using the ParMetis library

I am installing ParMetis 4.0.3 but into a non-default directory, with:
make config prefix=/My-ParMETIS-Directory/
And afterward:
make install
After that, I get a set of directories in that folder. And to write a program that uses it, I'm supposed to add the 'include "parmetis.h"' in the headline, and also add the 'libraries/binaries'. How exactly do I do the latter?
I'm just trying to get my code to compile now, and doing so I run:
g++ test.cpp
This is test.cpp:
#include<iostream>
#include "include/parmetis.h"
using std::cout;
using std::endl;
int main()
{
cout << "Test!" << endl;
return 0;
}
I keep getting "was not declared in this scope" for everything/every-line in parmetis.h.
How can I get test.cpp use the other folders/files that were installed?
You need to provide the location of your ParMetis library to
the compiler, since you have choosen to install the library
not in the default library directories.
mpic++ test.cpp -I /My-ParMETIS-Directory/ -I /My-METIS-Directory/
Edit#2:
What I did to get your code compiling:
Download the ParMetis library from webpage to /tmp
cd /tmp/
wget http://glaros.dtc.umn.edu/gkhome/fetch/sw/parmetis/parmetis-4.0.3.tar.gz
Unpack archive
tar -xf parmetis-4.0.3.tar.gz
Install ParMETIS into directory /tmp/parmetis
mkdir parmetis
cd parmetis-4.0.3/
make config prefix=/tmp/parmetis
make install
Install METIS into directory /tmp/metis
cd /tmp/
mkdir metis
cd parmetis-4.0.3/metis
make config prefix=/tmp/metis
make install
Compile test.cpp which is located in /tmp/
cd /tmp/
mpic++ test.cpp -I /tmp/parmetis -I /tmp/metis
when you coding wtih C++ ,if you meet question like that :
ccJjiCo.o: In function `main':
bsplele.cpp:(.text+0x45e): undefined reference to `METIS_PartMeshNodal'
collect2: ld returned 1 exit status
you can check the compiler sentence and you should paste the -lmetis at the end of the compilation command.It is useful for me.
my right compilation command is that:g++ -L/home/hadoop/metis/lib -I/home/hadoop/metis/include/ LL_metis.cpp -lmetis

How to install clang in custom location from SVN, making it recognize c++ standard library, on OS X

I try to compile and use clang from svn trunk. I basically try to follow the directions here:
svn co -q http://llvm.org/svn/llvm-project/llvm/trunk llvm
svn co -q http://llvm.org/svn/llvm-project/cfe/trunk llvm/tools/clang
svn co -q http://llvm.org/svn/llvm-project/clang-tools-extra/trunk llvm/tools/clang/tools/extra
svn co -q http://llvm.org/svn/llvm-project/compiler-rt/trunk llvm/projects/compiler-rt
mkdir llvm_build_release
cd llvm_build_release
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/usr/local -DLLVM_TARGETS_TO_BUILD=host ../llvm
make -j12
make install
Above, I configure clang to be installed in the custom location ~/usr/local since I want to be able to play with it without changing my default environment.
I then create a simple test.cpp:
#include <iostream>
int main(int argc, const char* argv[]){
std::cout << "Hello world\n";
return 0;
}
and try to compile it:
~/usr/local/bin/clang++ test.cpp -o test
but get the error message:
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
(using the system version of clang, the same compilation works fine).
If I manually enter which standard library to use, it does work
~/usr/local/bin/clang++ -std=c++11 -stdlib=libstdc++ -I/usr/include/c++/4.2.1/ -L/usr/lib test.cpp -o test
The question is: How do I configure, compile and install clang from source so that I do not have to enter these standard library settings, but instead can just enter the ordinary ~/usr/local/bin/clang++ test.cpp -o test? I have macports installed, with its version of the standard libraries and the include files, if that helps.

How to install CLang using precompiled binaries?

How do I install CLang on Ubuntu, using precompiled binaries of CLang that I downloaded?
Here's how I downloaded CLang: "LLVM Download Page" -> "Download LLVM 3.2" -> "Clang Binaries for Ubuntu-12.04/x86_64" ( http://llvm.org/releases/3.2/clang+llvm-3.2-x86_64-linux-ubuntu-12.04.tar.gz .)
Then, I expanded the archive into a folder on my Ubuntu 12.04 LTS 64-bit machine. The contents of the expanded folder look like this:
$ ls clang+llvm-3.2-x86_64-linux-ubuntu-12.04
bin docs include lib share
Question: What do I do next? Do I have to copy these into some folders myself, and if so, which ones exactly? Most instructions I found online are for building CLang from source, which doesn't apply here.
I am a newbie to most of these tools. I created a basic hello-world C++ program, and was able to compile and run it, using GCC and autotools. Now, I want to compile the same program with CLang.
You can follow the same step as mentioned in https://askubuntu.com/questions/89615/how-do-i-install-llvm-clang-3-0
using GNU tar:
wget <clang-binaries-tarball-url> # or `curl -O <url>`
tar xf clang*
cd clang*
sudo cp -R * /usr/local/
If your tar isn't GNU and
the archive you get is .tar.gz, you can use tar -xzf;
if you have .tar.xz archive, you can use tar -xJf;
for .tar.bz2 archive, you can use tar -xjf.
Assuming you compiled your program with g++ hello.cpp
The equivalents of gcc and g++ are clang and clang++ accordingly. They are found in the bin folder.
It doesn't matter where you place the folders of clang, what matters is you don't move them later. So place them somewhere (I prefer $HOME and I'll assume this for the next)
Then:
Prepend it to $PATH variable
export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:$PATH
Make this permanent by adding it to ~/.bashrc
echo "export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:\$PATH" >> ~/.bashrc
Now you can do clang++ hello.cpp
I would like to install clang in /home/s. i.e.,
/home/s
bin
lib
include
...
I did the following in Ubuntu:
wget <clang-binaries-tarball-url>
sudo tar -xf <clang+llvm-..tar.xz> --strip-components=1 -C /home/s
# Set the path environmental variable
export PATH=/home/s/bin:$PATH
# Tell ldconfig about new shared library in /home/s/lib
cd /home/s
cat > libs.conf << "END"
/home/s/lib
END
sudo mv libs.conf /etc/ld.so.conf.d/libs.conf
sudo ldconfig
To test it:
clang --version
The output is:
clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/s/bin
Let's test C++17 Filesystem withex1.cpp
#include <iostream>
#include <filesystem>
int main() {
for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
std::cout << file.path() << '\n';
}
}
Compile it
clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic ex1.cpp -o ex1 -lc++fs
Run it
./ex1
The output:
"./ex1"
"./ex1.cpp"