0

I have some linking problems with the current class extending rocksdb::Comparator.

"NumericKeyComparator.cpp"

#include <rocksdb/db.h>
#include <rocksdb/comparator.h>
#include "../../structure/structure.h"
#include "../../NumberToString.h"
#include <map>
#include <string>
#include "rocksdb/db.h"
#include "rocksdb/env.h"

namespace rocksdb {
    namespace {
        class NumericKeyComparator : public Comparator {
        public:
            ~NumericKeyComparator() {}

            NumericKeyComparator() {}

            // Three-way comparison function:
            // if a < b: negative result
            // if a > b: positive result
            // else: zero result
            virtual int Compare(const Slice &a, const Slice &b) const override {
                LONG_NUMERIC x, y;
                x = StringToNumber<LONG_NUMERIC>(std::string(a.data()));
                y = StringToNumber<LONG_NUMERIC>(std::string(b.data()));
                if (x == y) return 0;
                if (x < y) return -1;
                return 1;
            };

            virtual bool Equal(const Slice &a, const Slice &b) const override {
                return StringToNumber<LONG_NUMERIC>(std::string(a.data())) ==
                       StringToNumber<LONG_NUMERIC>(std::string(b.data()));
            }

            // Ignore the following methods for now:
            virtual const char *Name() const { return "NumericKeyComparator"; };

            virtual void FindShortestSeparator(std::string *, const Slice &) const override {};

            virtual void FindShortSuccessor(std::string *) const {};
        };
    }
}

const rocksdb::Comparator* nkcmp() {
    static rocksdb::NumericKeyComparator cmp;
    return &cmp;
}

In particular, the original class (provided by a .h file) is the following, equivalent to the class in LevelDB:

< rocksdb/comparator.h >

class Comparator {
 public:
  virtual ~Comparator();
  virtual int Compare(const Slice& a, const Slice& b) const = 0;
  virtual bool Equal(const Slice& a, const Slice& b) const {
    return Compare(a, b) == 0;
  }
  virtual const char* Name() const = 0;
  virtual void FindShortestSeparator(
      std::string* start,
      const Slice& limit) const = 0;
  virtual void FindShortSuccessor(std::string* key) const = 0;
  virtual const Comparator* GetRootComparator() const { return this; }
};

During the linking phase to the executable, the following error pertaining to the .o file is raised:

NumericKeyComparator.cpp.o:(.data.rel.ro+0x10): undefined rederence to "typeinfo for rocksdb::Comparator"

collect2: error: ld returned 1 exit status

Even though I think there is a problem on how I extended the class (but sincerly speaking, I do not know, since I've tried to follow the example provided in comparator_db_test.cc, the linkage is automagically provided by CMake as, by using the verbose flag, is revealed to be the following:

/usr/bin/c++ -fopenmp -lpthread -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lnuma -g CMakeFiles/.dir//main.cpp.o ... CMakeFiles/.dir//lib/serializer/rocksdb/comparators/NumericKeyComparator.cpp.o -o -L/usr/lib/x86_64-linux-gnu/libsnappy.so -L/usr/lib/x86_64-linux-gnu/liblz4.so -L/usr/local/lib/librocksdb.a -Wl,-rpath,/usr/lib/x86_64-linux-gnu/libsnappy.so:/usr/lib/x86_64-linux-gnu/liblz4.so:/usr/local/lib/librocksdb.a ../_tests/lib/googletest/googlemock/gtest/libgtest.a /usr/local/lib/libstxxl_debug.a -lpthread /usr/lib/x86_64-linux-gnu/libboost_graph.a /usr/lib/x86_64-linux-gnu/libboost_serialization.a /usr/lib/x86_64-linux-gnu/libboost_system.a /usr/lib/x86_64-linux-gnu/libboost_filesystem.a /usr/local/BerkeleyDB.6.2/lib/libdb-6.2.a -lrocksdb -lpthread -lm -lsnappy -llz4 -lz -lbz2 -lzstd

The Comparator class is virtual and provided within a header, so it should not be a linking problem. I even try to set the -frtti flags while compiling the .cpp file and even to move the class declaration to the .h itself, but nothing happened. I've been reading all the answers here on StackOverflow, but I did not find a solution.

jackb
  • 695
  • 8
  • 26

1 Answers1

0

see https://github.com/facebook/rocksdb/pull/3008 It seems that it's a bug.

hello.co
  • 746
  • 3
  • 21