我是C ++的新手,现在我想学习LevelDB,但是会遇到一些问题。
brew install leveldb --cc=gcc-4.8
我的代码:
#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++")
然后,这是错误:
/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
它可以在CentOS服务器上运行,但不能在我的Mac上运行,这是什么问题?
如果我手动编译它。gcc-4.8 main.cpp -L. -I./Users/apple/CLionProjects/leveldb/include -lpthread -lleveldb -lstdc++
收到警告:section "__textcoal_nt" is deprecated
,但如果我用gcc-4.8
替换gcc-5
,我收到错误:
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
为什么会有所不同?
答案 0 :(得分:0)
通常应使用find_package。但是在此答案出现时,无法通过find_package检测到levelDB。这是信息https://github.com/google/leveldb/issues/641
考虑对levelDB使用externalproject_add:https://github.com/ohhmm/openmind/blob/f1459119e8d36d6cc6edc776b2f242edaf51fecf/CMakeLists.txt#L292
或者仅将leveldb与pthread一起添加到TARGET_LINK_LIBRARIES。这适用于posix,但不适用于Windows。