Running a C++ program with Gradle

How can I build with Gradle 4+ a simple C++ project with the following components:

  • Two units with headers, e.g, unit1.h, unit1.cpp, unit2.h, unit2.cpp.
  • Two alternative main programs, e.g, main1.cpp, main2.cpp, which use unit1 and unit2.
  • I would like to build it using clang+±5.0, which is installed on my Ubuntu system.

What Gradle file should I use in order to build and run each main program in turn, e.g, like:

gradle run main1
gradle run main2

? I read the Gradle docs, and all I found was this example, but it is not explained so I did not understant how to change it to my project.