Compiling FIDL

Prerequisites

This tutorial builds on the Compiling FIDL tutorial. For more information on other FIDL tutorials, see the Overview.

Overview

This tutorial details how to include the HLCPP FIDL bindings into your code by creating a unit test that you can use as a “playground” for exploring the HLCPP bindings.

This document covers how to complete the following tasks:

The example code is located in your Fuchsia checkout in //examples/fidl/hlcpp/unittests/. If you want to write all the code as you follow this tutorial, you can remove the example code:

  1. rm -r examples/fidl/hlcpp/unittests/*

Write a C++ host test {#write-a-cpp-test}

  1. Add a gtest stub to examples/fidl/hlcpp/unittests/main.cc:

    1. #include <gtest/gtest.h>
    2. namespace {
    3. } // namespace
  2. Define a test and then create a dependency on the test through the $host_toolchain. To do this, add the following to examples/fidl/hlcpp/unittests/BUILD.gn:

    1. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/BUILD.gn" region_tag="first" %}
    2. test("example-cpp-host-test") {
    3. sources = [ "main.cc" ]
    4. deps = [ "//third_party/googletest:gtest_main" ]
    5. }
  3. Run the empty test suite:

    1. fx set core.x64 --with //examples/fidl/hlcpp/unittests
    2. fx test -vo example-cpp-host-test

    You should see test output indicating that zero tests have run, since no tests have been added yet.

Add the HLCPP FIDL bindings as a dependency {#add-dependency}

Add a dependency on the HLCPP bindings by referencing the FIDL target directly. The new test target should look like:

  1. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/BUILD.gn" region_tag="test" %}

(Optional) To view the newly generated bindings:

  1. Rebuild using fx build.
  2. Change to the generated files directory: out/default/fidling/gen/examples/fidl/fuchsia.examples/fuchsia/examples, where the generated files are located. You may need to change out/default if you have set a different build output directory. You can check your build output directory with cat .fx-build-dir.

For more information on how to find generated bindings code, see Viewing generated bindings code.

Include the HLCPP bindings in your C++ code {#include-hlcpp-bindings}

To include the bindings, add the following include statement to the top of examples/fidl/hlcpp/unittests/main.cc

  1. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/main.cc" region_tag="include" %}

Inspect and use the generated bindings code {#inspect-user-generated-bindings}

You can now write some tests by referring to the generated code. For more information on the bindings, see HLCPP Bindings Reference.

To get started, you can also use some example code. You can add this inside the anonymous namespace in main.cc:

  1. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/main.cc" region_tag="bits" %}
  2. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/main.cc" region_tag="enums" %}
  3. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/main.cc" region_tag="structs" %}
  4. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/main.cc" region_tag="unions" %}
  5. {%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/unittests/main.cc" region_tag="tables" %}

To rebuild and rerun the tests, run:

  1. fx test -vo example-cpp-host-test