如果我们在Linux Ubuntu上安装了FlatBuffers,我们将无法使用shortc flatc命令从任何地方进行编译,我们该如何解决?
例如:我想在package.json中使用一些命令 "flatc -o path/src/app/core/providers/flatbuffers ....."
为了实现这一点,仅仅安装FlatBuffers是不够的,我们还需要进行一些额外的操作-添加符号链接等。
如果我们在Linux Ubuntu上安装了FlatBuffers,我们将无法使用shortc flatc命令从任何地方进行编译,我们该如何解决?
例如:我想在package.json中使用一些命令 "flatc -o path/src/app/core/providers/flatbuffers ....."
为了实现这一点,仅仅安装FlatBuffers是不够的,我们还需要进行一些额外的操作-添加符号链接等。
针对Linux Ubuntu的Flatc和Flatbuffers解决方案:
include
目录复制到 /usr/include
才能使用 #include <flatbuffers/flatbuffers.h>
。 - hauroncmake -G "Unix Makefiles" .
进行编译(可能是由于较旧的 cmake 版本)。 - Yonatansudo apt install -y flatbuffers-compiler
- Evansudo apt update
sudo apt install -y flatbuffers-compiler
对于 Ubuntu 18.04 (bionic),您可以使用 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),但如果你不使用最新功能,那么这应该非常稳定。
cd flatbuffers
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
----------将以下内容保存到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)
对于Windows系统,您可以从https://github.com/google/flatbuffers/releases下载zip文件夹并解压缩。
删除以前的Flatc环境路径变量(如果存在),并添加Flatc.exe所在的提取路径。