open-source-parsers / jsoncpp

A C++ library for interacting with JSON.
Other
8.06k stars 2.63k forks source link

Undefined reference to things #1518

Closed AtomDRP closed 7 months ago

AtomDRP commented 9 months ago

Describe the bug Can't get this to compile. I have the file structure

├── json
│   ├── allocator.h
│   ├── assertions.h
│   ├── config.h
│   ├── forwards.h
│   ├── json_features.h
│   ├── json.h
│   ├── reader.h
│   ├── value.h
│   ├── version.h
│   └── writer.h
├── test.cpp
├── test.json

with test.cpp

#include <iostream>
#include <fstream>
#include "json/json.h"

int main()
{
    Json::Value data;
    std::ifstream infile("test.json", std::ifstream::binary);
    infile >> data;

    return 0;
}

and test.json

{
    "Anna":
    { 
        "age": 18,
            "profession": "student"
        },
    "Ben":
    {
        "age" : "nineteen",
        "profession": "mechanic"
    }
}

When I try g++ test.cpp, I get the following errors

:> g++ test.cpp
/usr/bin/ld: /tmp/ccXsOQrc.o: in function `main':
test.cpp:(.text+0x2f): undefined reference to `Json::Value::Value(Json::ValueType)'
/usr/bin/ld: test.cpp:(.text+0x66): undefined reference to `Json::operator>>(std::istream&, Json::Value&)'
/usr/bin/ld: test.cpp:(.text+0x89): undefined reference to `Json::Value::~Value()'
/usr/bin/ld: test.cpp:(.text+0xca): undefined reference to `Json::Value::~Value()'
collect2: error: ld returned 1 exit status

To Reproduce Steps to reproduce the behavior:

  1. Given the test.cpp and test.json files above, try g++ test.cpp

Expected behavior I expect this to compile without errors.

Desktop (please complete the following information):

k4p1cz commented 8 months ago

Same problem but on Windows

BillyDonahue commented 7 months ago

You don't have a compile error. You have a link error. You have only included a few of jsoncpp's headers. That's insufficient.

You have to install the whole library, and link your program to it. This is not a jsoncpp bug.

You just need some more experience with the idioms for using C++ libraries.