1Building {#flatbuffers_guide_building} 2======== 3 4## Building with CMake 5 6The distribution comes with a `cmake` file that should allow 7you to build project/make files for any platform. For details on `cmake`, see 8<https://www.cmake.org>. In brief, depending on your platform, use one of 9e.g.: 10 11 cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release 12 cmake -G "Visual Studio 10" -DCMAKE_BUILD_TYPE=Release 13 cmake -G "Xcode" -DCMAKE_BUILD_TYPE=Release 14 15Then, build as normal for your platform. This should result in a `flatc` 16executable, essential for the next steps. 17Note that to use clang instead of gcc, you may need to set up your environment 18variables, e.g. 19`CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -G "Unix Makefiles"`. 20 21Optionally, run the `flattests` executable from the root `flatbuffers/` 22directory to ensure everything is working correctly on your system. If this 23fails, please contact us! 24 25Building should also produce two sample executables, `flatsamplebinary` and 26`flatsampletext`, see the corresponding `.cpp` files in the 27`flatbuffers/samples` directory. 28 29*Note that you MUST be in the root of the FlatBuffers distribution when you 30run 'flattests' or `flatsampletext`, or it will fail to load its files.* 31 32### Make all warnings into errors 33 34By default all Flatbuffers `cmake` targets are build with `-Werror` flag. 35With this flag (or `/WX` for MSVC) C++ compiler will treat all warnings as errors. 36Additionally `-Wall -pedantic -Wextra` (or `/W4` form MSVC) flags are set. 37These flags minimize the number of possible defects in code and keep code highly portable. 38Using these flags is considered good practice but sometimes it can break dependent projects 39if a compiler is upgraded or a toolset is changed. 40Usually, newer compiler versions add new compile-time diagnostics that were unavailable before. 41These new diagnostic warnings could stop the build process if `-Werror` flag is set. 42 43It is possible to cancel `warnings as errors` flag at `cmake` configuration stage using 44`FLATBUFFERS_CXX_FLAGS` option. Compilation flags declared in `FLATBUFFERS_CXX_FLAGS` will be 45appended to the project-level `CMAKE_CXX_FLAGS` variable. 46Examples: 47 48- GCC and Clang: `cmake . -D FLATBUFFERS_CXX_FLAGS="-Wno-error"` 49- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/WX-"` 50- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/Wv <compiler.version>"` 51 52 53## Building with VCPKG 54 55You can download and install flatbuffers using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager: 56 57 git clone https://github.com/Microsoft/vcpkg.git 58 cd vcpkg 59 ./bootstrap-vcpkg.sh 60 ./vcpkg integrate install 61 ./vcpkg install flatbuffers 62 63The flatbuffers port in vcpkg is kept up to date by Microsoft team members and community contributors. 64If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository. 65 66## Building for Android 67 68There is a `flatbuffers/android` directory that contains all you need to build 69the test executable on android (use the included `build_apk.sh` script, or use 70`ndk_build` / `adb` etc. as usual). Upon running, it will output to the log 71if tests succeeded or not. 72 73You may also run an android sample from inside the `flatbuffers/samples`, by 74running the `android_sample.sh` script. Optionally, you may go to the 75`flatbuffers/samples/android` folder and build the sample with the 76`build_apk.sh` script or `ndk_build` / `adb` etc. 77 78## Using FlatBuffers in your own projects 79 80For C++, there is usually no runtime to compile, as the code consists of a 81single header, `include/flatbuffers/flatbuffers.h`. You should add the 82`include` folder to your include paths. If you wish to be 83able to load schemas and/or parse text into binary buffers at runtime, 84you additionally need the other headers in `include/flatbuffers`. You must 85also compile/link `src/idl_parser.cpp` (and `src/idl_gen_text.cpp` if you 86also want to be able convert binary to text). 87 88To see how to include FlatBuffers in any of our supported languages, please 89view the [Tutorial](@ref flatbuffers_guide_tutorial) and select your appropriate 90language using the radio buttons. 91 92### Using in CMake-based projects 93If you want to use FlatBuffers in a project which already uses CMake, then a more 94robust and flexible approach is to build FlatBuffers as part of that project directly. 95This is done by making the FlatBuffers source code available to the main build 96and adding it using CMake's `add_subdirectory()` command. This has the 97significant advantage that the same compiler and linker settings are used 98between FlatBuffers and the rest of your project, so issues associated with using 99incompatible libraries (eg debug/release), etc. are avoided. This is 100particularly useful on Windows. 101 102Suppose you put FlatBuffers source code in directory `${FLATBUFFERS_SRC_DIR}`. 103To build it as part of your project, add following code to your `CMakeLists.txt` file: 104```cmake 105# Add FlatBuffers directly to our build. This defines the `flatbuffers` target. 106add_subdirectory(${FLATBUFFERS_SRC_DIR} 107 ${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-build 108 EXCLUDE_FROM_ALL) 109 110# Now simply link against flatbuffers as needed to your already declared target. 111# The flatbuffers target carry header search path automatically if CMake > 2.8.11. 112target_link_libraries(own_project_target PRIVATE flatbuffers) 113``` 114When build your project the `flatbuffers` library will be compiled and linked 115to a target as part of your project. 116 117#### Override default depth limit of nested objects 118To override [the depth limit of recursion](@ref flatbuffers_guide_use_cpp), 119add this directive: 120```cmake 121set(FLATBUFFERS_MAX_PARSING_DEPTH 16) 122``` 123to `CMakeLists.txt` file before `add_subdirectory(${FLATBUFFERS_SRC_DIR})` line. 124 125#### For Google Play apps 126 127For applications on Google Play that integrate this library, usage is tracked. 128This tracking is done automatically using the embedded version string 129(flatbuffer_version_string), and helps us continue to optimize it. 130Aside from consuming a few extra bytes in your application binary, it shouldn't 131affect your application at all. We use this information to let us know if 132FlatBuffers is useful and if we should continue to invest in it. Since this is 133open source, you are free to remove the version string but we would appreciate 134if you would leave it in. 135