我是靠谱客的博主 粗犷蜡烛,最近开发中收集的这篇文章主要介绍etcd-cpp-apiv3 编译(备忘),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

etcd-cpp-apiv3

etcd-cpp-apiv3 是 C++ Etcd client api 库

见官方客户端库,推荐页: https://etcd.io/docs/v3.4/integrations/

Github 地址: https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3.git

etcd-cpp-apiv3 依赖:

  • boost
  • grpc
  • protobuf
  • cpprestsdk

由于官方 README.md 中的编译方式,并不是本人想要的方式

因此,花时间,编译了 boost 、 grpc 、protobuf 、cpprestsdk 、etcd-cpp-apiv3

结果,这个库,编译花了一天时间,因此必须做下备忘!

编译脚本

直接上编译脚本,流水账方式,记录了手动编译的整个过程:

#!/bin/bash

set -ex

# yum groupinstall -y "Development Tools" "Development Libraries"

# CMake
n=$(cmake --version | grep "3.20.1" | wc -l)
if [ "$n" == "0" ]; then
    mkdir -p /tmp/cmake
    pushd /tmp/cmake
    if [ ! -f "cmake-3.20.1.zip" ]; then
        wget https://cmake.org/files/v3.20/cmake-3.20.1.zip
        unzip cmake-3.20.1.zip
    fi
    cd cmake-3.20.1
    ./bootstrap --parallel=8
    gmake -j 8
    gmake install
    /usr/local/bin/cmake --version
    yum remove cmake -y
    ln -sf /usr/local/bin/cmake /usr/bin/
    cmake --version
    popd
fi

# openssl
if [ ! -d ./dep/openssl/ ]; then
    mkdir -p /tmp/
    pushd /tmp/
    if [ ! -d /tmp/openssl ]; then
        git clone -b OpenSSL_1_1_1k https://github.com/openssl/openssl.git
    fi
    if [ ! -d /tmp/openssl/my_build_out/ ]; then
        cd openssl
        mkdir -p my_build_out
        rm -rf my_build_out/*
        my_build_out=$PWD/my_build_out
        ./config --prefix=$my_build_out --openssldir=$my_build_out
        make -j 4
        make install
    fi
    popd
    mkdir -p ./dep/openssl/include
    mkdir -p ./dep/openssl/lib
    cp -rf /tmp/openssl/my_build_out/include/* ./dep/openssl/include
    cp -f /tmp/openssl/my_build_out/lib/*.a ./dep/openssl/lib
fi

# gRPC CPP
function cp_ex() {
    prefix=$1
    src_dir=$2
    dst_dir=$3
    echo $src_dir
    echo $dst_dir
    pushd $src_dir
    files=$(find ${prefix}/ -name "*.h")
    for file in ${files[@]}; do
        path=${file%/*}
        mkdir -p $dst_dir/$path
        cp -f $src_dir/$file $dst_dir/$file
    done
    files=$(find ${prefix}/ -name "*.inc")
    for file in ${files[@]}; do
        path=${file%/*}
        mkdir -p $dst_dir/$path
        cp -f $src_dir/$file $dst_dir/$file
    done
    popd
}
if [ ! -d ./dep/grpc ]; then
    mkdir -p /tmp/
    pushd /tmp/
    if [ ! -d /tmp/grpc ]; then
        git clone -b v1.38.0 https://github.com/grpc/grpc.git
        pushd grpc
        git submodule update --init
        popd
    fi
    cd grpc
    mkdir -p cmake/build
    mkdir -p my_build_out
    rm -rf my_build_out/*
    my_build_out=$PWD/my_build_out
    cd cmake/build
    cmake -DCMAKE_INSTALL_PREFIX=$my_build_out 
        -DCMAKE_BUILD_TYPE=Release 
        -DCMAKE_CXX_FLAGS="-fPIC" 
        -DCMAKE_C_FLAGS="-fPIC" 
        -DgRPC_SSL_PROVIDER="package" 
        -DOPENSSL_INCLUDE_DIR=/tmp/openssl/my_build_out/include 
        -DOPENSSL_LIBRARIES=/tmp/openssl/my_build_out/lib/libssl.a /tmp/openssl/my_build_out/lib/libcrypto.a 
        ../..
    make -j 4
    #make install ## CMake Error at cmake_install.cmake:2188 (file): file INSTALL cannot find "/tmp/grpc/cmake/modules/Findc-ares.cmake": No such file or directory.
    find third_party -name "*.a" | xargs -i cp {} .
    mkdir -p tmpo
    cp libabsl_*.a tmpo/
    cd tmpo
    find . -name "libabsl_*.a" | xargs -i ar x {}
    ar crsv libabsl.a *.o
    cd ..
    cp -f tmpo/libabsl.a .
    find . -name "address_sorting*.o" | xargs ar crsv libaddress_sorting.a
    popd
    mkdir -p ./dep/grpc/lib
    mkdir -p ./dep/grpc/include
    cp -f /tmp/grpc/cmake/build/*.a ./dep/grpc/lib/
    cp -rf /tmp/grpc/include/* ./dep/grpc/include/
    mkdir -p ./dep/protobuf/lib
    mkdir -p ./dep/protobuf/include
    tempdir=$PWD/./dep/protobuf/include
    cp_ex google /tmp/grpc/third_party/protobuf/src $tempdir
    mkdir -p ./dep/absl/lib
    mkdir -p ./dep/absl/include
    tempdir=$PWD/./dep/absl/include
    cp_ex absl /tmp/grpc/third_party/abseil-cpp/ $tempdir
    mv ./dep/grpc/lib/libabsl*.a ./dep/absl/lib/
    cp -f /tmp/grpc/cmake/build/grpc_*_plugin /usr/local/bin/
    cp -f /tmp/grpc/cmake/build/third_party/protobuf/protoc /usr/local/bin/
    rm -rf /usr/bin/protoc
    ln -sf /usr/local/bin/protoc /usr/bin/
    protoc --version
fi

# etcd-cpp-apiv3 -> dep boost
if [ ! -d ./dep/boost ]; then
    if [ ! -d /tmp/boost_1_74_0 ]; then
        mkdir -p /tmp/
        pushd /tmp/
        if [ ! -f ./boost_1_74_0.zip ]; then
            wget https://boostorg.jfrog.io/artifactory/main/release/1.74.0/source/boost_1_74_0.zip
        fi
        unzip boost_1_74_0.zip
        cd boost_1_74_0
        ./bootstrap.sh
        mkdir -p ./my_build_out
        ./b2 --prefix=$PWD/my_build_out --build-type=minimal 
            --with-system 
            --with-thread 
            --with-random 
            --with-chrono 
            --with-date_time 
            --with-atomic 
            --with-filesystem 
            --with-regex 
            install
        popd
    fi
    mkdir -p ./dep/boost/include
    mkdir -p ./dep/boost/lib
    cp -rf /tmp/boost_1_74_0/my_build_out/include/* ./dep/boost/include
    cp -f /tmp/boost_1_74_0/my_build_out/lib/*.a ./dep/boost/lib
fi

# etcd-cpp-apiv3 -> dep protobuf
if [ ! -d /tmp/grpc/third_party/protobuf/my_build_out ]; then
    pushd /tmp/grpc/third_party/protobuf
    ./autogen.sh
    mkdir -p my_build_out
    rm -rf my_build_out/*
    ./configure --prefix=$PWD/my_build_out --disable-shared
    make && make install
    popd
fi

# etcd-cpp-apiv3 -> dep cpprestsdk
if [ ! -f ./dep/cpprestsdk/lib/libcpprest.a ]; then
    if [ ! -d /tmp/cpprestsdk/ ]; then
        mkdir -p /tmp/
        pushd /tmp/
        git clone -b v2.10.18 https://github.com/microsoft/cpprestsdk.git
        popd
    fi
    pushd /tmp/cpprestsdk
    mkdir -p build
    mkdir -p my_build_out
    rm -rf my_build_out/*
    cd build
    cmake -DCMAKE_INSTALL_PREFIX=$PWD/../my_build_out 
        -DCPPREST_EXCLUDE_WEBSOCKETS=ON 
        -DBOOST_INCLUDEDIR=/tmp/boost_1_74_0/my_build_out/include 
        -DBOOST_LIBRARYDIR=/tmp/boost_1_74_0/my_build_out/lib 
        -DCMAKE_CXX_FLAGS="-w -fPIC -I/tmp/openssl/my_build_out/include/ -L/tmp/openssl/my_build_out/lib/" 
        -DCMAKE_C_FLAGS="-w -fPIC -I/tmp/openssl/my_build_out/include/ -L/tmp/openssl/my_build_out/lib/" 
        -DCMAKE_EXE_LINKER_FLAGS="-lssl -lcrypto" 
        -DBUILD_SHARED_LIBS=OFF 
        -DCMAKE_BUILD_TYPE=Release 
        -DOPENSSL_INCLUDE_DIR=/tmp/openssl/my_build_out/include/ 
        -DOPENSSL_LIBRARIES=/tmp/openssl/my_build_out/lib/libssl.a /tmp/openssl/my_build_out/lib/libcrypto.a 
        -DZLIB_INCLUDE_DIRS=/tmp/grpc/third_party/zlib 
        -DZLIB_LIBRARY=/tmp/grpc/cmake/build/libz.a 
        ..
    make -j 4 && make install
    popd
    mkdir -p ./dep/cpprestsdk/include
    mkdir -p ./dep/cpprestsdk/lib
    cp -rf /tmp/cpprestsdk/my_build_out/include/* ./dep/cpprestsdk/include
    cp -rf /tmp/cpprestsdk/my_build_out/lib64/*.a ./dep/cpprestsdk/lib
fi

# etcd-cpp-apiv3
if [ ! -d ./dep/etcd-cpp-apiv3 ]; then
    mkdir -p ./dep/etcd-cpp-apiv3/include
    mkdir -p ./dep/etcd-cpp-apiv3/lib
    mkdir -p /tmp/
    pushd /tmp/
    if [ ! -d /tmp/etcd-cpp-apiv3 ]; then
        git clone -b v0.2.1 https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3.git
    fi
    if [ ! -f /tmp/etcd-cpp-apiv3/my_build_out/lib64/libetcd-cpp-api.a ]; then
        cd etcd-cpp-apiv3
        mkdir -p build
        mkdir -p my_build_out
        rm -rf my_build_out/*
        mkdir -p /tmp/etcd-cpp-apiv3/proto/google/protobuf/
        cp -f /tmp/grpc/third_party/protobuf/my_build_out/include/google/protobuf/descriptor.proto /tmp/etcd-cpp-apiv3/proto/google/protobuf/
        cd build
        cmake -DCMAKE_INSTALL_PREFIX=$PWD/../my_build_out 
            -DBOOST_INCLUDEDIR=/tmp/boost_1_74_0/my_build_out/include 
            -DBOOST_LIBRARYDIR=/tmp/boost_1_74_0/my_build_out/lib 
            -DProtobuf_INCLUDE_DIR=/tmp/grpc/third_party/protobuf/my_build_out/include 
            -DProtobuf_LIBRARIES=/tmp/grpc/cmake/build/libprotobuf.a 
            -DGPR_LIBRARY=/tmp/grpc/cmake/build/libgpr.a 
            -DGRPC_LIBRARY=/tmp/grpc/cmake/build/libgrpc.a 
            -DGRPC_INCLUDE_DIR=/tmp/grpc/include 
            -DGRPC_GRPC++_REFLECTION_LIBRARY=/tmp/grpc/cmake/build/libgrpc++_reflection.a 
            -DGRPC_GRPC++_LIBRARY=/tmp/grpc/cmake/build/libgrpc++.a 
            -DGRPC_CPP_PLUGIN=/tmp/grpc/cmake/build/grpc_cpp_plugin 
            -DCPPREST_INCLUDE_DIR=/tmp/cpprestsdk/my_build_out/include 
            -DCPPREST_LIB=/tmp/cpprestsdk/my_build_out/lib64/libcpprest.a 
            -DCMAKE_CXX_FLAGS="-I/tmp/grpc/include -I/tmp/grpc/third_party/abseil-cpp -fPIC" 
            -DBUILD_SHARED_LIBS=OFF 
            ..
        make -j 4 && make install
    fi
    popd
    cp -rf /tmp/etcd-cpp-apiv3/my_build_out/include/* ./dep/etcd-cpp-apiv3/include
    cp -rf /tmp/etcd-cpp-apiv3/my_build_out/lib64/*.a ./dep/etcd-cpp-apiv3/lib
fi

其他说明

  • 编译 gRPC , make install 执行失败

    CMake Error at cmake_install.cmake:2188 (file):
      file INSTALL cannot find "/tmp/grpc/cmake/modules/Findc-ares.cmake": No
      such file or directory.
    

    因此,手动包括了 .a 等操作

  • 编译 etcd-cpp-apiv3 ,官方 CMakefile 对于指定第 3 方库分支,缺少几个 include 目录

    -DCMAKE_CXX_FLAGS="-I/tmp/grpc/include -I/tmp/grpc/third_party/abseil-cpp"
    

    因此,手动指定

  • 整合使用 etcd-cpp-apiv3 时,发现 grpc 与 cpprestsdk 使用的 openssl 不是同一家产品。因此需要编译 openssl ,并让他们使用同一份编译的

最后

以上就是粗犷蜡烛为你收集整理的etcd-cpp-apiv3 编译(备忘)的全部内容,希望文章能够帮你解决etcd-cpp-apiv3 编译(备忘)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部