Shravan40 Shravan40 - 26 days ago 9
C++ Question

undefined reference to `json11::Json::dump(std::string&) const' collect2: error: ld returned 1 exit status

I using json11 in my project

#include "crow_all.h"
#include "sqlOperations/sqlcpp_db_connection.cpp"
#include "date.h"
#include <json11.hpp>

int main() {
crow::SimpleApp app;
CROW_ROUTE(app,"/navOfFund")
.methods("POST"_method)
([](const crow::request& req){
auto x = crow::json::load(req.body);
if (!x)
return crow::response(400);
auto name = x["fund_name"].s();
std::ostringstream os;
vector<double>nav = select_from_fund_type(name);
json11::Json json(nav);
//std::cout << json.dump() << std::endl;
return crow::response{json.dump()};
});
crow::logger::setLogLevel(crow::LogLevel::Debug);

app.port(18080).multithreaded().run();
return 0;
}


And i am compiling my project using below command

g++ -std=c++1y -L/usr/local/lib -I ../../date main.cpp -ljson11 -lsqlpp-mysql -lmysqlclient -lboost_system -lpthread


And the error is

/tmp/ccqQDgWQ.o: In function `json11::Json::dump() const':
main.cpp:(.text._ZNK6json114Json4dumpEv[_ZNK6json114Json4dumpEv]+0x2c): undefined reference to `json11::Json::dump(std::string&) const'
collect2: error: ld returned 1 exit status


I am not able to find the root cause of error.

But i don't know why, this works perfectly fine.

#include <iostream>
#include <json11.hpp>

int main()
{
std::vector<double> test_vector { 0.1, 0.2, 0.3, 0.4, 0.5 };
json11::Json json(test_vector);

std::cout << json.dump() << std::endl;
}


And compiling using

g++ -std=c++1y json_test.cpp -ljson11

Answer

The function void dump(std::string &out) isn't defined in the .hpp file, see here.
Instead it is defined in the .cpp file, that is here.

You should rather do this:

g++ -std=c++1y -L/usr/local/lib -I ../../date main.cpp json11.cpp -ljson11 -lsqlpp-mysql -lmysqlclient -lboost_system -lpthread