Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

HELP needed to get started

Help
Malooh
2012-05-30
2013-06-12
  • Malooh
    Malooh
    2012-05-30

    Hi,

    I would like to use libjson but I'm having trouble getting started.

    I understand that we can use libjson in 2 ways:

    (a) If we turn on JSON_LIBRARY in JSONOptions.h and do "make" followed by "make install", libjson will be installed, and is available for use by all the users in C programs only (ie C++ interface is not available).

    (b) Alternatively, if we unzip the downloaded zip file in our own directory and turn off JSON_LIBRARY in JSONOptions.h, we can then use it in C++ programs.

    Is my above undestandings correct?

    I did (a) above (turned on JSON_LIBRARY in JSONOptions.h), and the C header file /usr/local/include/libjson/libjson.h and the object library /usr/local/lib/libjson.a are installed, as expected. However, when I then tried out the "Simple Write Example" in the "Getting Started" directory, I got undefined references. My "Simple Write Example" C program, named w.c,  is as follow:

    #include <libjson/libjson.h>
    #include <stdio.h>

    int main()
    {
        JSONNODE *n = json_new(JSON_NODE);
        json_push_back(n, json_new_a("String Node", "String Value"));
        json_push_back(n, json_new_i("Integer Node", 42));
        json_push_back(n, json_new_f("Floating Point Node", 3.14));
        json_push_back(n, json_new_b("Boolean Node", 1));
        json_char *jc = json_write_formatted(n);
        printf("%s\n", jc);
        json_free(jc);
        json_delete(n);
    }

    The command I used to compile and link the C program is:

    gcc -DNDEBUG -I/usr/local/include -L/usr/local/lib -ljson w.c

    I got the following error messages:

    /tmp/ccS0NiZ8.o: In function `main':
    w.c:(.text+0x19): undefined reference to `json_new'
    w.c:(.text+0x30): undefined reference to `json_new_a'
    w.c:(.text+0x3f): undefined reference to `json_push_back'
    w.c:(.text+0x53): undefined reference to `json_new_i'
    w.c:(.text+0x62): undefined reference to `json_push_back'
    w.c:(.text+0x78): undefined reference to `json_new_f'
    w.c:(.text+0x87): undefined reference to `json_push_back'
    w.c:(.text+0x9b): undefined reference to `json_new_b'
    w.c:(.text+0xaa): undefined reference to `json_push_back'
    w.c:(.text+0xb5): undefined reference to `json_write_formatted'
    w.c:(.text+0xce): undefined reference to `json_free'
    w.c:(.text+0xd9): undefined reference to `json_delete'
    collect2: ld returned 1 exit status

    Where did I go wrong?

    I then did (b) (unzipped the downloaded zip file in my work directory and turn off JSON_LIBRARY in JSONOptions.h, then do "cd libjson; make"), and tried the C++ "Simple Write Example", I got the following errors:

    /tmp/ccomC4RI.o: In function `main':
    u.cpp:(.text+0xe6): undefined reference to `JSONNode::JSONNode(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*)'
    u.cpp:(.text+0x173): undefined reference to `JSONNode::JSONNode(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
    u.cpp:(.text+0x202): undefined reference to `JSONNode::JSONNode(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, double)'
    u.cpp:(.text+0x28f): undefined reference to `JSONNode::JSONNode(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
    /tmp/ccomC4RI.o: In function `JSONNode::JSONNode(char)':
    u.cpp:(.text._ZN8JSONNodeC1Ec+0x14): undefined reference to `internalJSONNode::newInternal(char)'
    /tmp/ccomC4RI.o: In function `internalJSONNode::makeUnique()':
    u.cpp:(.text._ZN16internalJSONNode10makeUniqueEv+0x23): undefined reference to `internalJSONNode::newInternal(internalJSONNode const&)'
    /tmp/ccomC4RI.o: In function `JSONNode::push_back(JSONNode const&)':
    u.cpp:(.text._ZN8JSONNode9push_backERKS_+0x21): undefined reference to `internalJSONNode::push_back(JSONNode const&)'
    /tmp/ccomC4RI.o: In function `JSONNode::write_formatted(unsigned int) const':
    u.cpp:(.text._ZNK8JSONNode15write_formattedEj+0x93): undefined reference to `internalJSONNode::Write(unsigned int, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) const'
    /tmp/ccomC4RI.o: In function `JSONNode::decRef()':
    u.cpp:(.text._ZN8JSONNode6decRefEv+0x2d): undefined reference to `internalJSONNode::deleteInternal(internalJSONNode*)'
    collect2: ld returned 1 exit status

    My C++ "Simple Write Example" program, named u.cpp, is as follow:

    #include <iostream>
    #include <string>
    #include <memory>
    #include "libjson/libjson.h"
    using namespace std;
    int main()
    {
        JSONNode n(JSON_NODE);
        n.push_back(JSONNode("String Node", "String Value"));
        n.push_back(JSONNode("Integer Node", 42));
        n.push_back(JSONNode("Floating Point Node", 3.14));
        n.push_back(JSONNode("Boolean Node", true));
        std::string jc = n.write_formatted();
        std::cout << jc << std::endl;
    }

    The command I issued to compile and link the C++ "Simple Write Example" program was:

    g++ -DNDEBUG -Llibjson -ljson u.cpp

    Again, where did I go wrong?

    Any help is appreciated.

     
  • Malooh
    Malooh
    2012-05-30

    Re the C++ "Simple Write Example" program in the above email, if I compile and link with "g++ -DNDEBUG libjson/Objects_static/JSONNode.o libjson/Objects_static/internalJSONNode.o libjson/Objects_static/JSONWorker.o libjson/Objects_static/JSONChildren.o u.cpp", most of the error messages disappear, but I still get:

    /tmp/ccQ1bhJn.o: In function `JSONNode::write_formatted(unsigned int) const':
    u.cpp:(.text._ZNK8JSONNode15write_formattedEj+0x93): undefined reference to `internalJSONNode::Write(unsigned int, bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) const'
    collect2: ld returned 1 exit status

    The problem is: internalJSONNode::Write is undefined. "nm libjson/Objects_static/internalJSONNode.o | fgrep -i write" confirms that it's indeed undefined.

     
  • lijo
    lijo
    2012-11-20

    Please try the command,

    g++ -DNDEBUG u.cpp -Llibjson -ljson

    Alternatively, you could also use,

    g++ -DNDEBUG u.cpp libjson/libjson.a

    Issue is in the order of arguments on your command line.  When you link to static libraries, those libraries should always come after the objective files(or even other libs), which uses those libraries. This limitation is arising from the way linker processes the translation units. For a detailed discussion see http://stackoverflow.com/questions/2624238/c-undefined-references-with-static-library

     
  • You need to include libjson in the linker

     
  • yvette
    yvette
    2013-02-05

    I think you did not build c library in rigth way, this message following may help you.
    "
    I had to comment out this line in makefile

       #cp -rv $(srcdir)/Dependencies/ $(include_path)/$(libname_hdr)/$(srcdir)

    to get past the Dependencies error, and I used this command to build and install shared libraries in /usr/local/lib

    SHARED=1 make && sudo SHARED=1 prefix=/usr/local make instal"
    http://sourceforge.net/projects/libjson/forums/forum/1119662/topic/5389211