14

if we install flatbuffers on Linux Ubuntu, we will not be able to use the shortc flatc command from anywhere to compile, how can we do this?

For example: i want use in package.json some command "flatc -o path/src/app/core/providers/flatbuffers ....."

in order to do this not enough to install flatbuffers, we also need to carry out a number of additional manipulations - add a symbolic link and so on.

  • 1
    Hi! Give some examples of what you tried to install the software, what you expected to be the desired behavior (working) and what was the result (not working). – Spyros K Mar 28 '19 at 09:58
  • This question has nothing to do with FlatBuffers, as the answer is the same for any binary you compile yourself on Linux. And your answer is not great, having your system dirs link to a git folder. Modifying PATH may be another option, or copying.. Frankly you are better off not having it available everywhere, since to use its output you need the rest of the dir anyway. Might as well use scripts that point to the FlatBuffers dir. – Aardappel Mar 29 '19 at 15:39

5 Answers5

39

solution for flatc and flatbuffers for linux ubuntu :

  1. choice "folder for installation"
  2. cd "folder for installation"
  3. git clone https://github.com/google/flatbuffers.git
  4. cd flatbuffers
  5. cmake -G "Unix Makefiles" (install cmake if need)
  6. make
  7. sudo ln -s /full-path-to-flatbuffer/flatbuffers/flatc /usr/local/bin/flatc
  8. chmod +x /full-path-to-flatbuffer/flatbuffers/flatc
  9. run in any place as "flatc"
  • 1
    Worked for me, it's the first google search result too. I also needed to copy `include` dir to `/usr/include` to be able to `#include `. – hauron May 11 '19 at 09:32
  • 1
    7. sudo ln -s $(realpath $(find . -name "flatc")) /usr/local/bin/flatc – Ivan Kovtun Jul 01 '19 at 18:00
  • perhaps make install between the make and link creation would also put the header files in the right place, etc. – kalyanswaroop Mar 03 '20 at 20:08
  • In my case, I had to compile with `cmake -G "Unix Makefiles" .` (perhaps due to an old version of cmake) – Yonatan Jul 07 '20 at 09:30
  • Note that Ubuntu packages flatbuffers for 20.04 (and maybe others). See @Gea-Suan Lin's answer below. `sudo apt install -y flatbuffers-compiler` – Evan Nov 30 '20 at 22:15
  • Was getting this error when I try 'make'. make[2]: *** [CMakeFiles/flatbuffers.dir/build.make:161: CMakeFiles/flatbuffers.dir/depend] Error 1 make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/flatbuffers.dir/all] Error 2 make: *** [Makefile:146: all] Error 2 – Nappa Jan 05 '22 at 12:50
11

In Ubuntu 20.04 (focal) it's in apt repository already (https://packages.ubuntu.com/focal/flatbuffers-compiler) so you can just install it:

sudo apt update
sudo apt install -y flatbuffers-compiler

And for Ubuntu 18.04 (bionic) you can use PPA (https://launchpad.net/~hnakamur/+archive/ubuntu/flatbuffers):

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

Both are a little old (1.11), but if you don't use latest features, then this should be quite solid.

Gea-Suan Lin
  • 598
  • 7
  • 14
  • Thanks! This is definitely better than building it manually and having to manually manage subsequent updates. – Evan Nov 30 '20 at 22:14
10

The answer by Olexandr is only for the flatc binary.

To build & install all of flatbuffers from source using cmake, follow the instructions here:

  • Clone the repo & cd flatbuffers
  • Generate build files for linux ubuntu
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
  • Compile
make
  • Install
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
Corey Cole
  • 2,262
  • 1
  • 26
  • 43
1

Here is the CMake file to make life bit easy. These cmake commands will download the flatbuffer from git repo & install on the linux system wide access. To install after build run "sudo make install".

----------Save the below content into CMakeLists.txt file & run 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)
Mav
  • 11
  • 1
1

For windows, you can download the zip folder from https://github.com/google/flatbuffers/releases and extract it.
Delete the previous environment path variables for flatc (if present) and add the extracted path where flatc.exe is present.

Nappa
  • 131
  • 1
  • 9