6

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.

Nizar B.
  • 3,098
  • 9
  • 38
  • 56
  • The "Building/Testing" section (from the README you linked to) instructs you to use the `python scons.py platform=your-platform-here` to build the library, but you're showing a `Makefile`. Is the build script you posted for your application, or for the `jsoncpp` library? – André Caron Mar 30 '12 at 18:09

1 Answers1

3

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.

Collin
  • 11,977
  • 2
  • 46
  • 60
  • Hi Andre, I've uses the scons.py script to build with 'CHECK' target and all is okay with building + testing. It's my own MAKEFILE. – Nizar B. Mar 30 '12 at 18:29
  • Hi Collin, I've put the -I lines in my CXXFLAGS and here is somes new errors that I get : ` ./src/ModConnection.o: In function `main': ModConnection.cpp:(.text+0x16): undefined reference to `Json::Value::Value(Json::ValueType)' ` – Nizar B. Mar 30 '12 at 18:34
  • Now, I don't have error with my compilation since I've followed the advices of Collins Hockey, But when I'm writting this in my main program : `Json::Value root;` I always get this error: `ModConnection.cpp:(.text+0x16): undefined reference to `Json::Value::Value(Json::ValueType)' ModConnection.cpp:(.text+0x27): undefined reference to `Json::Value::~Value()'` – Nizar B. Mar 30 '12 at 18:49
  • I've updated my answer with some linking fixes that should get you going. I can't try building this right now because my cygwin install is horribly broken, but I can try later tonight if it doesn't work fo r you. – Collin Mar 30 '12 at 18:50
  • Thanks Collin ! get no error this time but I come back to you if I meet another one linked to my JSONCPP library compilation. – Nizar B. Mar 30 '12 at 19:31
  • So now when I'm launching my binaries, I've this error : ./server: error while loading shared libraries: libjson_linux-gcc-4.5.1_libmt.so: cannot open shared object file: No such file or directory – Nizar B. Mar 30 '12 at 20:13
  • `g++` is automatically linking to the shared library. You can do one of two things. 1 - Copy the `libjson_linux-gcc-4.5.1_libmt.so` file from the build directory to the place where your executable is, or 2 - add `-static` to your `LDFLAGS`, this will force g++ into statically linking the library, that is, putting all the needed functionality directly into your executable file. – Collin Mar 31 '12 at 02:56