Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JSON-CPP?

Tags:

c++

json

jsoncpp

I've strictly followed this documentation to install and use jsoncpp library in my project : jsoncpp README

But I still have this problem with my compilation:

g++ -W -Wall -Werror -c -o src/ModConnection.o src/ModConnection.cpp src/ModConnection.cpp:15:23: fatal error: json/json.h: No such file or directory compilation terminated.

It's happen when I'm trying to use #include <json/json.h>

Here is my Linux MAKEFILE :

CXX     =       g++
NAME    =       bin/server
SRCS    =       ./src/ModConnection.cpp\
                ./src/unixNetwork.cpp
OBJS    =       $(SRCS:.cpp=.o)
CXXFLAGS +=     -W -Wall -Werror
LDFLAGS =       -L ./src/jsoncpp-src-0.5.0/buildscons/linux-gcc4.5.1/src/lib_json/libjson_linux-gcc-4.5.1_libmt.a -I src/jsoncpp-src-0.5.0/include
RM      =       rm -f
$(NAME) :       $(OBJS)
$(CXX) $(LDFLAGS) -o $(NAME) $(OBJS)

all     :       $(NAME)

clean   :
                $(RM) $(OBJS)

fclean  :       clean
                $(RM) $(NAME)

re      :       fclean all

.PHONY  :       all clean fclean re

Thanks for you help.

like image 760
Nizar B. Avatar asked Mar 30 '12 17:03

Nizar B.


1 Answers

You're specifying the include directory for jsoncpp in your LDFLAGS variable, but those don't get used until you've already compiled the individual cpp files. You need to put the part -I src/jsoncpp-src-0.5.0/include somewhere in the flags which get added to the compile lines, such as CXXFLAGS.

To expand a bit, you're using implicit Make rules to build your individual .cpp files, then you have a specific target for building your application out of those objects.

See the GNU Make Catalog of Rules for more info, but the one you're using is here:

Compiling C++ programs n.o is made automatically from n.cc, n.cpp, or n.C with a recipe of the form $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c. We encourage you to use the suffix ‘.cc’ for C++ source files instead of ‘.C’.

Edit: Now for your linking errors.

You're getting these problems because the linker can't find the actual implementations of the functions you're calling.

First, your -L directive needs to point to a folder, not a library. -L sets a search path for libraries. It should be set to the folder where the library the jsoncpp build was created. Next, you must link the library itself. That library name is gigantic, but adding -l json_linux-gcc-4.5.1_libmt to LDFLAGS should do the trick. -l (that's lower ell) sets an actual library to link.

like image 60
Collin Avatar answered Oct 17 '22 20:10

Collin