Today we’ll continue the series by addressing a nagging problem: how do we build the C++ plugin? With C# we inherit, for better or worse, Unity’s build system where we just edit .cs files and press the play button. This doesn’t work with C++, so we’ll need to build something similar that’s just as easy to use.

Table of Contents

First off, this build system is very basic. Then again, so is Unity’s build system. In a way, basic is a good thing.

For iOS, we don’t have a problem. C++ files in a Unity project’s Assets directory are simply added to the Xcode project and built along with the IL2CPP output. There’s no need for a build system at all! Of course we don’t want to support only iOS, so we’ll need something for other platforms.

One approach would be to create makefiles and IDE projects for every possible OS and IDE. That’s pretty onerous though, so we won’t take that approach. Instead, we’ll generate those build scripts and projects using a widely-used tool called CMake. That’ll allow us to make a single build script and run CMake on it to generate whatever kind of build scripts or IDE projects we want.

First we place a CMakeLists.txt file in our Assets directory. This file tells CMake where our source is, what language it’s in, what features we need, and so forth. Users of the C++ scripting system never need to modify this file. Instead, they just run a few simple commands:

# Make a directory to build in
mkdir cmake
 
# Go to the build directory
cd cmake
 
# Build the Xcode project
cmake -G "Xcode" /path/to/unity/project/Assets

CMake has now created an Xcode project, in this example. You can use it just like any other Xcode project. Just open the NativeScript.xcodeproj file and click “Product > Build”.

The power of CMake is in the -G "Xcode" part of that command. That tells CMake to build an Xcode project, but we could have just as easily told it to build makefiles for a command-line build instead:

# Make a directory to build in
mkdir cmake
 
# Go to the build directory
cd cmake
 
# Build the makefiles
cmake -G "Unix Makefiles" /path/to/unity/project/Assets

Then just type make to build the C++ plugin. No matter what “generator” you specify with -G, the resulting project should build the C++ plugin the same way.

The same goes for cross-compiling to Android. Just add -DANDROID_NDK=/path/to/android/ndk to tell it where your Android NDK is installed and you’re ready to go.

CMake works on Windows, too. You can use it with any version of Microsoft Visual Studio, even the free Community version. Just specify -G "Visual Studio 15 2017 Win64". Older versions work, too.

This means we can go back to the C++ workflow we outlined in part two of this series:

  1. Edit C++ sources
  2. Compile C++
  3. Switch to the Unity editor window
  4. Run the game

The vague “Compile C++” step is now more concrete: build however is appropriate for your CMake generator. Since we’ll probably use an IDE like Xcode or Visual Studio and just click a “build” button, the steps are just these:

  1. Edit C++ sources
  2. Click “build” in your IDE
  3. Switch to the Unity editor window
  4. Run the game

With this build system in place, scripting in C++ is now quite easy regardless of which platform you’re working on.