Search code examples
c++makefilejsoncpp

Error while including the jsoncpp lib


I am trying to parse JSON with C++. My Makefile looks as follows:

LDFLAGS = -L/home/ting/Temp/code/jsoncpp/libs/linux-gcc-5.4.0/ ./jsoncpp/libs/linux-gcc-5.4.0/libjson.a ./jsoncpp/libs/linux-gcc-5.4.0/libjson.so
INC = -I/home/ting/Temp/code/jsoncpp/include/json

CC=g++
CFLAGS = -std=c++11


main: main.cpp
    $(CC) -o $@ $(LDFLAGS) $(INC) $^ ${CFLAGS}

When I simply #include "json.h", the compiler gives me bunches of errors. I just picked some of them:

/usr/include/x86_64-linux-gnu/c++/5/bits/os_defines.h:44:19: error: 
missing binary operator before token "("
 #if __GLIBC_PREREQ(2,15) && defined(_GNU_SOURCE)
               ^
In file included from /usr/include/c++/5/cwchar:44:0,
             from /usr/include/c++/5/bits/postypes.h:40,
             from /usr/include/c++/5/iosfwd:40,
             from /usr/include/c++/5/ios:38,
             from /usr/include/c++/5/istream:38,
             from /usr/include/c++/5/fstream:38,
             from main.cpp:1:
/usr/include/wchar.h:74:43: error: missing binary operator before token "("

It does not look like the libs have any error. But this problem only appears when I link to the jsoncpp lib and add its include dir in the Makefile.

I am so confused; what happened?


Solution

  • Your jsoncpp includes have to be like this:

    #include <json/json.h>
    

    and your include path have to end at include dir, this way:

    INC = -I/home/ting/Temp/code/jsoncpp/include
    

    If you omit the json dir from the include, and add it in your INC variable, the compiler will end up picking a features.h header from the json directory, instead of the required features.h of glibc, which yields errors like the one you posted (note that features.h in glibc defines that __GLIBC_PREREQ macro).