Using libjson in a C++ project - c++

I'm trying to use libjson within a C++ project and the docs tell me to just "add libjson's source to your project, comment JSON_LIBRARY in the JSONOptions.h file and any C++ compiler should compile it."
Being quite new to C++ and all that, how exactly am I supposed to do that (not using any IDE)? Should I just #include the libjson.h file and that's it? Shouldn't I reference libjson somehow in my call to g++ when compiling my project?
thx in advance

If you go into the libjson library folder, you will see a makefile. Navigate to that directory in a terminal and type:
make
then
make install
Then, in your code
#include <libjson.h>
or, depending on your include path:
#include <libjson/libjson.h>
That should be all that you need to do.
If you need additional help, you can post in the help forum at sourceforge (I am the author of libjson)

You have to:
One,
#include <libjson.h>
in order to get access to the functions and data types the library offers, then
Two, link against the libjsonz library:
g++ -o myprogram myprogram.c -ljson
(the -ljson flag has to come last or you'll get a linker error with never versions of GCC.)
EDIT: if you need to build the library, you typically have a configure script or a Makefile. See how to use them.

if you install json you should find include file at /usr/local/include
so
#include <json/json.h>
gcc exasmple.c -ljson

Related

C++ Link external libraries

i just started with c++ programming.
For my new work, i have to download, install and take use of an external library. It is called ICE.
It was composed as a .tar file, so i decomposed it inside my home-directory "/home/foo/ice".
Now, there is the directory: "/home/foo/ice/src", within all the .h headers, i need for the program.
But can i tell the compiler, where he can find all these new headers?
I mean only with #include, he obviously doesn't know.
What i need:
#include <image.h>
"image.h" is inside "/home/foo/ice/src"
Greetings
If you have gcc compiler you can use -I option.
From the manual :
-I dir: Add the directory dir to the list of directories to be searched for header files.
So for you it should be something like this:
g++ myprog.cpp -I /home/foo/ice/src -o myprog
But it is better to install the library, you should have some readme.txt or INSTALL file about how to do this..
You asked about linking a library, but your description shows that you have problems with the include path, which klm123 answered already.
The library paths for linking is another option, usually -Llibpath
It may help to check the options of you compiler, here for example the Directory Options of GCC

Compiler option for missing include file on Linux

It's been a while since I've dealt with C/C++, so forgive me if this is a ridiculously easy to answer question - I just don't quite know how to "Google" it.
I have a file, "MyFile.h" that includes file "includedFile.h". However, the compiler cannot find the file. Please see below picture:
What I'm doing is moving the project from an old Solaris box to a Linux box. The weird thing is that it worked on the Solaris box as-is but Linux is a little confused.
The makefile that I use for the project hasn't changed either which makes me think that it may be a compiler option...
So how do I tell the compiler on Linux where that include file is, or how do I specify it in "MyFile.h?"
With gcc and clang, you specify the include path using -I:
g++ -o myprogram main.cc extra.cc -I/usr/include/boost -I/my/extra/include/files
You can specify full paths in your files, as in #include "/path/to/my/includedfile.h", but I strongly discourage this as it forces everyone who wants to compile your code to comply with that directory layout.
Also relevant: Read the following link for the difference between #include <file> and #include "file" in gcc: http://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html
Assuming you are using g++, you pass a path with the -I flag.
g++ ..... -I<a path to your includes> -I<another path to includes>

Creating shared library containing other libraries

i've written a class from that i want to create a shared library. But this class uses other libraries. The call that i use to generate the library is of the form
g++ -fpic -c [necessary *.cpp files] [necessary includes]
Unfortunately this call leads to an error, namely iostream.h: No such file or directory.
But when i build a testfile using the library code it compiles and works properly.
Where might be the problem?
Thanks for your help.
Firstly, I'm pretty sure that you should be using iostream rather than iostream.h so you could try changing that in your source code. E.g.
#include <iostream>
Secondly, you need to check the include directives on the compilation line, as what you're getting is a compilation error indicating that the preprocessor can't find this file. As you haven't listed either your failed or successful commands here, I can't add much more than that.

Why can't g++ find iostream.h?

I'm trying to understand how to compile C++ programs from the command line using g++ and (eventually) Clang on Ubuntu.
I found a webpage which explains MakeFiles and I am following their directions. http://mrbook.org/tutorials/make/
I downloaded the four example files into their own directory.
main.cpp
hello.cpp
factorial.cpp
functions.h
I then went ahead and ran their example of how to manually compile without a MakeFile.
g++ main.cpp hello.cpp factorial.cpp -o hello
When I ran the command from above, I received the following error from g++:
main.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.
hello.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.
My only experience with writing c++ is using an IDE such as VS C++ Express or CodeBlocks. Isn't the compiler supposed to know what iostream.h is and where to find it?
How do I get rid of this error so the program willl compile?
Thanks for any help.
Before the C++ language was standardized by the ISO, the header file was named <iostream.h>, but when the C++98 standard was released, it was renamed to just <iostream> (without the .h). Change the code to use #include <iostream> instead and it should compile.
You'll also need to add a using namespace std; statement to each source file (or prefix each reference to an iostream function/object with a std:: specifier), since namespaces did not exist in the pre-standardized C++. C++98 put the standard library functions and objects inside the std namespace.
<iostream.h> has never been a standard C++ header, because it did not make it into the C++ standard.
Instead we got <iostream>, in 1998.
Steer well clear of teaching material using non-standard stuff such as <iostream.h> or void main.
However, as a practical solution for your current pre-standard code, you may try to replace
#include <iostream.h>
with
#include <iostream>
using namespace std;
It’s not guaranteed to work, but chances are that it will work.
Another related issue that wasn't mentioned here, so I will include it for anyone's future reference, is from the command line the compiler needs the environment path variable updated to find the location of the c++ header files. In windows you can just update the path environment using the 'advanced system properties' GUI and add the location of the c++ include files. This will update the PATH environment variable in Windows cmd & Cygwin automatically upon restarting the shell.
To update your PATH from Linux or the Cygwin shell type...
PATH=$PATH:/your_path_here
Example:PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/c++
Also a good idea to add just the include directory as well:
PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/
...or check the proper directories for the location of your installation's include files, I recommend installing mingw for use with Cygwin, which is envoked with g++.
To install additional needed packages in Cygwin re-run the Cygwin install utility & check install from Internet to add packages from web repositories and add mingw-gcc-g++ & mingw-binutils.
To compile: g++ hello.cpp -o hello
If using the gcc utility instead compile with the command:
gcc hello.cpp -o hello -lstdc++
... to get your executable.
As long as you have either gcc or mingw installed and the path to the c++ include files is in your path environment variable, the commands will work.

How do I use a shared library (in this case JsonCpp) in my C++ program on Linux?

I'm a new-ish C++ programmer, and I'm doing my first program on my own using C++. I decided I would like to use JSON to store some of the data I'm going to be using, and I've found a library to handle JSON, JsonCpp.
I've installed the library using my Linux system's package manager, and in my C++ code, I've used in my source code file
#include <json>
and compiled it using g++ and it's -ljson and -L/usr/lib options (libjson.so is located in /usr/lib).
However, the first usage of Json::Value, an object provided by the library, gives a compilation error of "Json has not declared". I'm sure my mistake is something simple, so could someone explain what I'm doing wrong? None of the books I had mention how to use shared libraries, so I've had to google to find this much.
EDIT: g++ with the -E option gives this error:
json: no such file or directory.
I checked the file list of JsonCPP:
include/json/autolink.h [code]
include/json/config.h [code]
include/json/features.h [code]
include/json/forwards.h [code]
include/json/json.h [code]
include/json/reader.h [code]
include/json/value.h [code]
include/json/writer.h [code]
Try #include <json/json.h> if the headers are installed in /usr/include. If they're installed somewhere else, mention this path with -I
g++ -I/my/lib/include -L/my/lib/lib -lmylib mysource.cpp
Did you also tell g++ where to find the header files via -I - this would be my guess at the problem.
That error pretty nearly certainly implies that you do not have the #include in the actual source file that gets the error.
use
g++ -E [whatever other options]
to see the cpp output would be one way to check and see what you've actually included.
It might help if you actually paste the error message; your comment suggests an error on the #include, but your question suggests something else.