1

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.

Mark
  • 113
  • 7
  • You should use `-I` and `-L` to specify the header and library search path. – for_stack Feb 07 '22 at 06:24
  • Note that [YEDIS is deprecated][1] and shouldn't be used for new projects. I'd suggest either YCQL or YSQL. [1]: https://docs.yugabyte.com/latest/yedis/ – dh YB Feb 07 '22 at 09:27
  • If you can consider using other Redis clients, Aedis simplifies sending STL containers to Redis, see for example the example in the Overview section here: https://mzimbres.github.io/aedis/ – mzimbres Nov 05 '22 at 19:16

0 Answers0