0

Easy and trivial problem probably, im trying to use the nlohmann library for Json in c++ in a easy test to later on use it on a harder problem. My test is:

#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>

using namespace std;
using json = nlohmann::json;

int main()
{
    json j;         // Create a json object
    ifstream jfile("test.json");
    jfile >> j;     // Read json file as file stream
    float pi = j.at("pi");
    bool happy = j.at("happy");
    return 0;
}

the json is this simply one but is not importante since i cannot compile the .cc

{
    "pi":3.1415,
    "happy":true
}

Im using gcc. I got the .cc in a folder and all the include file i need for nlohmann library in a subfolder of that one, im trying to use the command -I and -L but every time i compile with simething like this

gcc -Wall -o prova -I/include prova.cc

or like this

gcc -Wall -o prova -L/include prova.cc

the problems are the same and the output is:

prova.cc: In function ‘int main()’:
prova.cc:13:8: warning: unused variable ‘pi’ [-Wunused-variable]
  float pi = j.at("pi");
        ^~
prova.cc:14:7: warning: unused variable ‘happy’ [-Wunused-variable]
  bool happy = j.at("happy");
       ^~~~~
/tmp/cc4K5F28.o: In function `main':
prova.cc:(.text+0x46): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)'
prova.cc:(.text+0x6e): undefined reference to `std::allocator<char>::allocator()'
prova.cc:(.text+0x8b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
prova.cc:(.text+0xc5): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
prova.cc:(.text+0xd4): undefined reference to `std::allocator<char>::~allocator()'
prova.cc:(.text+0xe3): undefined reference to `std::allocator<char>::allocator()'
prova.cc:(.text+0x100): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
prova.cc:(.text+0x136): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
prova.cc:(.text+0x145): undefined reference to `std::allocator<char>::~allocator()'
prova.cc:(.text+0x159): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
prova.cc:(.text+0x194): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
prova.cc:(.text+0x1a8): undefined reference to `std::allocator<char>::~allocator()'
prova.cc:(.text+0x1bc): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
prova.cc:(.text+0x1d0): undefined reference to `std::allocator<char>::~allocator()'
prova.cc:(.text+0x1e4): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
/tmp/cc4K5F28.o: In function `__static_initialization_and_destruction_0(int, int)':
prova.cc:(.text+0x23b): undefined reference to `std::ios_base::Init::Init()'
prova.cc:(.text+0x250): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc4K5F28.o: In function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider::~_Alloc_hider()':
prova.cc:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderD2Ev[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderD5Ev]+0x14): undefined reference to `std::allocator<char>::~allocator()'

it goes on a lot more but i cut here because the problem is for sure the inlcude file wich is not found.

For sure this is a newbie question but i lost already enough time looking around on the net for tutorial on that library or similar problems. So thanks to anyone.

  • GCC have two compiler frontend-programs: `gcc` for C programs, and `g++` for C++ programs. – Some programmer dude Nov 01 '21 at 13:59
  • 2
    Also, the `-I` flag (upper.case i) is to add an **i**nclude path, to search for header files; And `-L` adds a **l**ibrary path to search for libraries to link with (added with the `-l` (lower-case L) option). – Some programmer dude Nov 01 '21 at 14:00

0 Answers0