My C++ skills is limited and I'm trying to save data to redis/yedis in C++ by following the guide from https://docs.yugabyte.com/latest/yedis/develop/client-drivers/yedis/cpp/#write-the-helloworld-c-application. The example code I am using is:
#include <cpp_redis/cpp_redis>
#include<iostream>
#include<vector>
#include<string>
#include<utility>
using namespace std;
int main() {
cpp_redis::client client;
client.connect("127.0.0.1", 6379, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
if (status == cpp_redis::client::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});
string userid = "1";
vector<pair<string, string>> userProfile;
userProfile.push_back(make_pair("name", "John"));
userProfile.push_back(make_pair("age", "35"));
userProfile.push_back(make_pair("language", "Redis"));
// Insert the data
client.hmset(userid, userProfile, [](cpp_redis::reply& reply) {
cout<< "HMSET returned " << reply << ": id=2, name=John, age=35, language=Redis" << endl;
});
// Query the data
client.hgetall(userid, [](cpp_redis::reply& reply) {
std::vector<cpp_redis::reply> retVal;
if (reply.is_array()) {
retVal = reply.as_array();
}
cout << "Query result:" <<endl;
for (int i = 0; i < retVal.size(); i=i+2) {
cout << retVal[i] << "=" <<retVal[i+1] << endl;
}
});
// synchronous commit, no timeout
client.sync_commit();
return 0;
}
but I'm encountering the error below, which seems like anything that has to do with cpp_redis are not defined:
/usr/bin/ld: /tmp/ccX9Gfa1.o: in function `main::{lambda(cpp_redis::reply&)#2}::operator()(cpp_redis::reply&) const':
ybredis_hello_world.cpp:(.text+0xad): undefined reference to `operator<<(std::ostream&, cpp_redis::reply const&)'
/usr/bin/ld: /tmp/ccX9Gfa1.o: in function `main::{lambda(cpp_redis::reply&)#3}::operator()(cpp_redis::reply&) const':
ybredis_hello_world.cpp:(.text+0x10c): undefined reference to `cpp_redis::reply::is_array() const'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x11c): undefined reference to `cpp_redis::reply::as_array() const'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x19d): undefined reference to `operator<<(std::ostream&, cpp_redis::reply const&)'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x1d2): undefined reference to `operator<<(std::ostream&, cpp_redis::reply const&)'
/usr/bin/ld: /tmp/ccX9Gfa1.o: in function `main':
ybredis_hello_world.cpp:(.text+0x266): undefined reference to `cpp_redis::client::client()'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x2d8): undefined reference to `cpp_redis::client::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, std::function<void (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, cpp_redis::client::connect_state)> const&, unsigned int, int, unsigned int)'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x4ac): undefined reference to `cpp_redis::client::hmset(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&, std::function<void (cpp_redis::reply&)> const&)'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x4ea): undefined reference to `cpp_redis::client::hgetall(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<void (cpp_redis::reply&)> const&)'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x508): undefined reference to `cpp_redis::client::sync_commit()'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x53a): undefined reference to `cpp_redis::client::~client()'
/usr/bin/ld: ybredis_hello_world.cpp:(.text+0x666): undefined reference to `cpp_redis::client::~client()'
collect2: error: ld returned 1 exit status
I get the above error when I run:
g++ -ltacopie -lcpp_redis -std=c++11 -o ybredis_hello_world ybredis_hello_world.cpp
Initally I have my project structure as follows:
--/home/doug/yuga
--cpp_redis
--ybredis_hello_word.cpp
but I noticed the example code includes cpp_redis
with #include <cpp_redis/cpp_redis>
instead of #include "cpp_redis/cpp_redis"
so I thought I needed to have the cpp_redis in the system directory, ie where iostream
and vector
etc are located. I therefore went to /usr/include/c++/9
and installed cpp_redis
there.
However when I try to compile it I still get the same error. I'm not quite sure what I'm doing wrong.