Day 5 : 12 June 2022 : CMake on Windows

 My 100 Daze of Code

https://github.com/davidjwalling/100-days-of-code

Day 5 : 12 June 2022 : CMake on Windows

So far, we've seen using CMake on Linux and MacOS. Also, we've looked at native compilation on Windows using Visual Studio (VS). Today, we'll look at using CMake on Windows. I decided to devote an entire day (hour) to this because there are several ways to invoke CMake on Windows, using different "Generators". A generator will write the inputs to the native build tools.

But Wait, There's More ...

Before heading to CMake, I wanted to first point out that VS' native tools can build just fine without using CMake, of course. To build using "msbuild," open an x64 Native Tools Command Prompt for Visual Studio - this is provided when VS is installed. Invoke msbuild and pass the solution name and the desired platform and configuration.

msbuild ava.sln /p:Platform=x64 /p:Configuration=Debug

Repeat for Debug and Release configurations for Win32 and x64 platforms as desired. To reproduce this output, first clean the solution if you've already built with the IDE.


Updating the CMakeLists.txt

Building for Windows with CMake will require that we add some additional information to our CMakeLists.txt file. We want to specify the same preprocessor directives that we did in our .vcxproj files (which are not used by CMake). So, we've added the following section at the end of CMakeLists.txt. Note that "target_compile_definition" allows us to direct the definition to a specific target.


CMake with the Native Tools Generator

Using CMake, we specify the desired generator with the -G option. Here, we ask Visual Studio itself to generate the inputs for the build. This can be done from a standard command prompt as Administrator.

We'll clean and recreate the build folder, run CMake to generate input files, run CMake to build the project output and copy the compiled binaries into installation folders. We'll need to make sure our installation folder is part of our path so we can run ava.exe.


The first invocation of CMake generates input files in the build folder.


The second invocation of CMake builds and installs.


The "install" step created the "C:/Program Files/ava/bin" folder and stored ava.exe there. It also created a "lib" folder and stored the library there. However, it does not update the environment so that these binaries can be found. We have to make sure the "bin" folder is in our path and that the correct version of the library is loaded.



CMake with the "NMake Makefiles" Generator

We can also have CMake generate input for use by the "NMake" Program Maintenance Utility provided as part of Visual Studio. Here, we inform CMake of the desired platform and configuration first, then in the second step, we run nmake instead of CMake.

Here, open an x64 Native Tools Command Prompt for Visual Studio as Administrator so that it has permission to install the generated binaries.

Next we run CMake specifying the "NMake Makefiles" generator.


Next we run "nmake install". Curiously, NMake install is C:/Program Files (x86) and not C:/Program files.


CMake with the "Ninja" Generator

Ninja is a small build generator available at ninja-build.org. Here use it as the generator option when building our input files using CMake. Download the ninja.exe binary from https://github.com/ninja-build/ninja/releases and save it in your path. Restart your Native Tools Command Prompt if it is already opened so that Ninja can be detected.

Run these commands in the x64 Native Tools Command Prompt as Administrator.

cmake -G "Ninja"


cmake --build . --config Debug --target install



So, to recap our first five days, we've defined a simple C++ library and executable and shown several build and debugging steps on our first three platforms: Linux, macOS and Windows.

In our next post, we'll take our first step into mobile development, building Ava on iOS. To do that, we'll need to setup a developer account at developer.apple.com, create a certificate, app and provisioning profile, create a simple Swift-UI driver app using Xcode on macOS, create an Objective C wrapper around our libava IDriver class, build and install on iOS 15.5 and single-step debug using Xcode. That will probably take more than one day!
 




Comments

Popular posts from this blog

Day 12 : 19 June 2022 : Adding Windows Service Logic

Day 11: 18 June 2022 : Handling Program Arguments