0

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?

underscore_d
  • 6,309
  • 3
  • 38
  • 64
J.R.
  • 769
  • 2
  • 10
  • 25
  • If you want help with your code, post it. For all we know, you're not closing a namespace somewhere, or any other number of syntactical errors that can cascade to the seemingly meaningless fragment of error you posted. – underscore_d Nov 06 '17 at 19:25
  • In general, choosing “some” of the errors isn’t that useful, because a lot of errors can cause other errors in seemingly unrelated parts of the code. Instead, you should pick the *first* error, which has a much better chance of being the actual cause. – Daniel H Nov 06 '17 at 19:51
  • @underscore_d hi. code is pretty simple. I just included the header. #include "json.h". Then I had over several pages error. This is also the reason why I stated "some errors", cause I cannot understand what GCC wanted to say. – J.R. Nov 07 '17 at 07:48
  • @DanielH Hi. What I posted is actually the first error, cause I got erros over pages and I cannot understand what GCC wanted to say. So I picked the words "some" – J.R. Nov 07 '17 at 07:49
  • side note: It's more idiomatic to `#include` system headers with ``, rather than `"quotes"`. This at least makes your intent clearer and easier to tell between local vs system headers, and it can also alter the search order (though I don't think that's precisely defined). – underscore_d Nov 07 '17 at 11:20

1 Answers1

1

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).

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35