Last active
December 23, 2021 06:21
-
-
Save KuanYuChang/f768cd48e1f2e261109a1e6bfa5a07b5 to your computer and use it in GitHub Desktop.
Minimal Work Example of building program with LevelDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # If using macOS, please first install Command-Line Tools, Homebrew, and cmake via the following commands: | |
| # $ xcode-select --install | |
| # $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
| # $ brew install cmake | |
| # declear vars | |
| WORK_SPACE=$(pwd) | |
| REPO_DIR=${WORK_SPACE}/remote_repo | |
| PROJECT_DIR=${WORK_SPACE}/my_project | |
| LEVELDB_REPO=${REPO_DIR}/leveldb | |
| LEVELDB_BUILD=${LEVELDB_REPO}/build | |
| LEVELDB_INSTALL=${PROJECT_DIR}/local | |
| # clone and build LevelDB | |
| mkdir -p ${REPO_DIR} | |
| git clone --recurse-submodules https://github.com/google/leveldb.git ${LEVELDB_REPO} | |
| mkdir -p ${LEVELDB_BUILD} && cd ${LEVELDB_BUILD} | |
| cmake -DCMAKE_INSTALL_PREFIX=${LEVELDB_INSTALL} -DCMAKE_BUILD_TYPE=Release ${LEVELDB_REPO} | |
| make install -j4 | |
| # create several source files for testing | |
| ## subdirs | |
| mkdir -p ${PROJECT_DIR}/include/mwe | |
| mkdir -p ${PROJECT_DIR}/src/mwe | |
| ## include/mwe/test.h | |
| echo '#ifndef INCLUDE_MWE_TEST_H_ | |
| #define INCLUDE_MWE_TEST_H_ | |
| void test_leveldb (); | |
| #endif // INCLUDE_MWE_TEST_H_ | |
| ' | tee ${PROJECT_DIR}/include/mwe/test.h | |
| ## src/mwe/test.cc | |
| echo '#include "leveldb/db.h" | |
| #include "mwe/test.h" | |
| void test_leveldb () { | |
| leveldb::DB* db; | |
| leveldb::Options options; | |
| options.create_if_missing = true; | |
| leveldb::Status status = leveldb::DB::Open(options, "testdb", &db); | |
| assert(status.ok()); | |
| } | |
| ' | tee ${PROJECT_DIR}/src/mwe/test.cc | |
| ## src/main.cc | |
| echo '#include <iostream> | |
| #include "mwe/test.h" | |
| int main() { | |
| test_leveldb(); | |
| std::cout << "Build LevelDB Successed." << std::endl; | |
| return 0; | |
| } | |
| ' | tee ${PROJECT_DIR}/src/main.cc | |
| ## CMakeLists.txt | |
| echo 'cmake_minimum_required(VERSION 3.9) | |
| project(my_project) | |
| set(CMAKE_CXX_STANDARD 11) | |
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | |
| set(CMAKE_CXX_EXTENSIONS OFF) | |
| include_directories(include local/include) | |
| file(GLOB_RECURSE SOURCES src/*.cc) | |
| add_executable(main ${SOURCES}) | |
| find_package(Threads REQUIRED) | |
| find_library (LEVELDB | |
| NAMES leveldb libleveldb # what to look for | |
| HINTS ${CMAKE_SOURCE_DIR}/local/lib # where to look | |
| NO_DEFAULT_PATH) # do not search system default paths | |
| target_link_libraries(main PRIVATE Threads::Threads ${LEVELDB}) | |
| ' | tee ${PROJECT_DIR}/CMakeLists.txt | |
| # compile and build with LevelDB | |
| cd ${PROJECT_DIR} | |
| mkdir build && cd build | |
| cmake ${PROJECT_DIR} && make -j4 | |
| # execute and test | |
| ./main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment