CMake is the game changer?


My CMake experience is fairly low. During my career years, IDE is always doing all heavy lifting. Build and run with GUI button in IDE not allow me to be exposed command line build world. My first experience with CMake was during evaluating among unit test framework for C/C++, mainly C++ production code but with low level driver C code from a vendor.
The first candidate was googletest(gtest). Google test(gtest) is C++ unit test framework. Another candidate was CPPUTest. They are both xUnit Test Framework. However, in embedded system projects, both C and C++ are used and googletest needs to write every test in C++ way and mocking is required to write the class as a unit not a function. Also, to run googletest, it is required to know how to use CMake, which manages to write CMakeList.txt customizing build environment such as which compiler to use and dependency setup. However, CPPUTest provides multiple sample projects per IDE and each function can be mocked without introducing a class. Less learning curve for CPPUTest at this stage!
When the project is related to safety critical system, the compiler is often required to be certified by the certain level of safety. In other words, unit test project also needs to build with the specific compiler. My project was using IAR workbench and its compiler(iccarm). In the past, IAR did not have many command line supports. Because of this eco system, I have used CPPUTest instead of googletest in the past. Pairing CPPUTest with IAR project has one disadvantage. Each unit test project needs to build in IAR project. It ends up with too many IAR projects to handle.
There is no tutorial about how to build the project using CMake instead of using IAR’s own command. Recently, the demand of Visual Studio Code and CI/CD is significantly raised. IAR has started to support Visual Studio Code Plug in and you can build and debug there instead of running IAR Workbench IDE. IAR toolchain also has evolved and CI server does not require to install whole IDE but IAR Build Tools(only compiler and toolchain) to be able to build binary with CMake. You can find this information here.
Building C/C++ project with command line in Windows is not always the best situation. Because my development is all done in IAR workbench last decade, I never need to learn CMake. The cross-platform compatibility is a significant and growing trend in various fields. CMake is one of them. It provides cross compiling and allow user to build within any OS.
Next, I would like to learn how CMake works. To familiar with CMake, I decide to run googletest with Visual Studio Code because of “According to the 2024 Stack Overflow Developer Survey, Visual Studio Code (VS Code) is the most popular integrated development environment (IDE), with 73.6% of developers reporting they use it.” It took long time to set up everything in Windows. I want to share all steps below:
Visual Studio Code[Download for Windows]
Git
Run regular terminal to test git version by command below
git --versionMinGW via MYSY2[Download installer]
Run mysy2 terminal and install the MinGW-w64 toolchain by command below
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
Add environment variables to add MinGW as C:\msys64\ucrt64\bin
Run regular terminal to test compiler version by command below
gcc --version
g++ --version
gdb --versionCMake[Under Binary Distributions: Windows x64 Installer]
Run regular terminal to test cmake version by command below
cmake --versionClone googletest by command below
git clone https://github.com/google/googletest.git
The goal is running the first google test sample: my_project
I went through every steps but there were issues to build.
Cannot identify C compiler
Cannot identify C++ compiler
NMake is not found
First two items can be fixed by updating CMakeList.txt. Add two lines between first two lines:
Third item can be fixed by adding parameter in cmake:
Tutorial runs command in my_project folder and I run in build folder because there are few more steps to run application and those steps are done in build folder. Once, this build worked. Let’s run cmake --build command.
Now, check the exe file and run it.
Happy testing in Windows!
Subscribe to my newsletter
Read articles from Hyunwoo Choi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
