【问题标题】:How to install flatc and flatbuffers on linux ubuntu如何在 linux ubuntu 上安装 flatc 和 flatbuffers
【发布时间】:2019-03-28 09:48:49
【问题描述】:

如果我们在 Linux Ubuntu 上安装 flatbuffers,我们将无法从任何地方使用 shortc flatc 命令进行编译,我们该怎么做?

例如:我想在 package.json 中使用一些命令 “flatc -o path/src/app/core/providers/flatbuffers .....”

为了做到这一点还不足以安装flatbuffers,我们还需要进行一些额外的操作——添加符号链接等等。

【问题讨论】:

  • 嗨!举例说明您尝试安装软件的内容、您期望的期望行为(有效)以及结果如何(无效)。
  • 这个问题与 FlatBuffers 无关,因为对于你在 Linux 上自己编译的任何二进制文件,答案都是一样的。你的答案不是很好,让你的系统目录链接到一个 git 文件夹。修改 PATH 可能是另一种选择,或者复制。坦率地说,你最好不要让它随处可用,因为要使用它的输出,你无论如何都需要目录的其余部分。不妨使用指向 FlatBuffers 目录的脚本。

标签: linux ubuntu flatbuffers


【解决方案1】:

在 Ubuntu 20.04 (focal) 中,它已经在 apt 存储库中 (https://packages.ubuntu.com/focal/flatbuffers-compiler),因此您可以安装它:

sudo apt update
sudo apt install -y flatbuffers-compiler

对于 Ubuntu 18.04(仿生),您可以使用 PPA (https://launchpad.net/~hnakamur/+archive/ubuntu/flatbuffers):

sudo apt-add-repository ppa:hnakamur/flatbuffers
sudo apt update
sudo apt install -y flatbuffers-compiler

两者都有点旧(1.11),但如果你不使用最新的功能,那么这应该是相当可靠的。

【讨论】:

  • 谢谢!这绝对比手动构建它并且必须手动管理后续更新要好。
【解决方案2】:

linux ubuntu 的 flatc 和 flatbuffers 解决方案

  1. 选择“安装文件夹”
  2. cd "安装文件夹"
  3. git 克隆https://github.com/google/flatbuffers.git
  4. cd 平面缓冲区
  5. cmake -G "Unix Makefiles"(如果需要,安装 cmake)
  6. 制作
  7. sudo ln -s /full-path-to-flatbuffer/flatbuffers/flatc /usr/local/bin/flatc
  8. chmod +x /full-path-to-flatbuffer/flatbuffers/flatc
  9. 以“flatc”身份在任何地方运行

【讨论】:

  • 为我工作,它也是第一个谷歌搜索结果。我还需要将include 目录复制到/usr/include 才能复制到#include <flatbuffers/flatbuffers.h>
  • 7. sudo ln -s $(realpath $(find . -name "flatc")) /usr/local/bin/flatc
  • 也许 make install 在 make 和链接创建之间也会将头文件放在正确的位置,等等。
  • 在我的情况下,我必须使用cmake -G "Unix Makefiles" . 进行编译(可能是由于旧版本的 cmake)
  • 请注意,Ubuntu 为 20.04(可能还有其他)打包了平面缓冲区。请参阅下面的@Gea-Suan Lin 的回答。 sudo apt install -y flatbuffers-compiler
【解决方案3】:

Olexandr 的答案仅适用于 flatc 二进制文件。

要使用 cmake 从源代码构建和安装所有平面缓冲区,请按照说明 here

  • 克隆repo & cd flatbuffers
  • 为 linux ubuntu 生成构建文件
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
  • 编译
make
  • 安装
sudo make install
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/include/flatbuffers
-- Installing: /usr/local/include/flatbuffers/stl_emulation.h
-- Installing: /usr/local/include/flatbuffers/flexbuffers.h
-- Installing: /usr/local/include/flatbuffers/minireflect.h
-- Installing: /usr/local/include/flatbuffers/flatbuffers.h
-- Installing: /usr/local/include/flatbuffers/pch
-- Installing: /usr/local/include/flatbuffers/pch/flatc_pch.h
-- Installing: /usr/local/include/flatbuffers/pch/pch.h
-- Installing: /usr/local/include/flatbuffers/flatc.h
-- Installing: /usr/local/include/flatbuffers/code_generators.h
-- Installing: /usr/local/include/flatbuffers/util.h
-- Installing: /usr/local/include/flatbuffers/grpc.h
-- Installing: /usr/local/include/flatbuffers/base.h
-- Installing: /usr/local/include/flatbuffers/registry.h
-- Installing: /usr/local/include/flatbuffers/hash.h
-- Installing: /usr/local/include/flatbuffers/reflection_generated.h
-- Installing: /usr/local/include/flatbuffers/idl.h
-- Installing: /usr/local/include/flatbuffers/reflection.h
-- Installing: /usr/local/lib/cmake/flatbuffers/FlatbuffersConfig.cmake
-- Installing: /usr/local/lib/cmake/flatbuffers/FlatbuffersConfigVersion.cmake
-- Installing: /usr/local/lib/libflatbuffers.a
-- Installing: /usr/local/lib/cmake/flatbuffers/FlatbuffersTargets.cmake
-- Installing: /usr/local/lib/cmake/flatbuffers/FlatbuffersTargets-release.cmake
-- Installing: /usr/local/bin/flatc
-- Installing: /usr/local/lib/cmake/flatbuffers/FlatcTargets.cmake
-- Installing: /usr/local/lib/cmake/flatbuffers/FlatcTargets-release.cmake

【讨论】:

    【解决方案4】:

    这里是 CMake 文件,让生活更轻松。这些 cmake 命令将从 git repo 下载 flatbuffer 并安装在 linux 系统范围内。在构建运行“sudo make install”后安装。

    ---------将以下内容保存到CMakeLists.txt文件中并运行cmake-----------

    cmake_minimum_required(VERSION 3.11)
    cmake_policy(SET CMP0048 NEW)
    
    project(flatbuffers-external
        VERSION 2.0.0
        DESCRIPTION "Flatbuffers Build"
    )
    
    option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." OFF)
    option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
           OFF)
    option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
           OFF)
    option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" OFF)
    
    
    include(FetchContent)
    FetchContent_Declare(
        flatbuffers_cpp
        GIT_REPOSITORY  https://github.com/google/flatbuffers.git
        GIT_TAG         v2.0.0
        PREFIX            "${PROJECT_SOURCE_DIR}/tp"
        SOURCE_DIR        "${PROJECT_SOURCE_DIR}/tp/fb"
        BINARY_DIR        "${PROJECT_SOURCE_DIR}/tp/fb-build"
    )
    
    FetchContent_MakeAvailable(flatbuffers_cpp)
    

    【讨论】:

      【解决方案5】:

      对于 Windows,您可以从 https://github.com/google/flatbuffers/releases 下载 zip 文件夹并解压缩。
      删除之前用于 flatc 的环境路径变量(如果存在)并添加提取的 flatc.exe 所在的路径。

      【讨论】:

        猜你喜欢
        • 2013-01-25
        • 1970-01-01
        • 2015-07-14
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        相关资源
        最近更新 更多