2

I am new to C++, and now I want to learn LevelDB, but get some issues.

brew install leveldb --cc=gcc-4.8

  • my code:

    #include <iostream>
    #include "leveldb/db.h"
    int main() {
        leveldb::Options options;
        leveldb::DB *db;
        options.create_if_missing = true;
        leveldb::Status status = leveldb::DB::Open(options, "./.db", &db);
        assert(status.ok())
        std::cout << "Hello, World!" << std::endl;
        return 0;
    }
    
  • CMakeLists.txt

    cmake_minimum_required(VERSION 3.6)
    project(ldb)
    set(CMAKE_CXX_STANDARD 14)
    
    find_library(LevelDB_LIBRARY leveldb)
    
    set(SOURCE_FILES main.cpp)
    add_executable(ldb ${SOURCE_FILES})
    
    target_link_libraries(ldb /usr/local/lib/libleveldb.a)
    
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads REQUIRED)
    TARGET_LINK_LIBRARIES(ldb pthread)
    
    set(CMAKE_VERBOSE_MAKEFILE TRUE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libstdc++")
    

then, here is the errors:

    /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/ldb.dir/link.txt --verbose=1
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -v -stdlib=libstdc++ -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/ldb.dir/main.cpp.o  -o ldb /usr/local/lib/libleveldb.a -lpthread 
    Apple LLVM version 8.1.0 (clang-802.0.41)
    Target: x86_64-apple-darwin16.5.0
    Thread model: posix
    InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    clang: warning: libstdc++ is deprecated; move to libc++ [-Wdeprecated]
    "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o ldb -search_paths_first -headerpad_max_install_names CMakeFiles/ldb.dir/main.cpp.o /usr/local/lib/libleveldb.a -lpthread -lstdc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
    Undefined symbols for architecture x86_64:
      "snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)", referenced from:
          leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
      "snappy::RawUncompress(char const*, unsigned long, char*)", referenced from:
          leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
      "snappy::MaxCompressedLength(unsigned long)", referenced from:
          leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
      "snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)", referenced from:
          leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[2]: *** [ldb] Error 1
    make[1]: *** [CMakeFiles/ldb.dir/all] Error 2
    make: *** [all] Error 2

it works on CentOS server,but not work on my Mac,what's the problem?

if I compile it by hand.gcc-4.8 main.cpp -L. -I./Users/apple/CLionProjects/leveldb/include -lpthread -lleveldb -lstdc++ got a warning: section "__textcoal_nt" is deprecated, but if i replace gcc-4.8 with gcc-5, i got an error :

    Undefined symbols for architecture x86_64:
    "leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)", referenced from:
          _main in ccWAzDAN.o
    ld: symbol(s) not found for architecture x86_64
    collect2: error: ld returned 1 exit status

Why is it different?

jian gao
  • 53
  • 6
  • Does it compile if you compile it by hand without cmake? – dtell Apr 17 '17 at 09:37
  • @datell I am not compile it by hand, I use CLion. – jian gao Apr 17 '17 at 11:46
  • Then just try to compile it by hand. You seem to miss the library you are trying to link. Check in /usr/local/lib is its there. – dtell Apr 17 '17 at 11:53
  • The line `ld: symbol(s) not found for architecture x86_64` is just a *brief description* of previous errors detected by the linker. Please, provide *actual error* reports, which contains **exact names of missed symbols**. – Tsyvarev Apr 17 '17 at 12:26
  • @Tsyvarev thank you. – jian gao Apr 18 '17 at 02:59
  • Possible duplicate of [Compile leveldb c++ program in linux error?](http://stackoverflow.com/questions/28411875/compile-leveldb-c-program-in-linux-error) – Tsyvarev Apr 18 '17 at 08:15

1 Answers1

0

Usually find_package should be used. But levelDB is not detectable with find_package at the moment of this answer. Here is the info https://github.com/google/leveldb/issues/641

Consider using externalproject_add for he LevelDB: https://github.com/ohhmm/openmind/blob/f1459119e8d36d6cc6edc776b2f242edaf51fecf/CMakeLists.txt#L292

Alternatively just add leveldb to TARGET_LINK_LIBRARIES together with pthread. This works for posix but not for Windows though.

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54