Cross Platform C++ Development without CMake
Photo by Pixabay: https://www.pexels.com/photo/view-of-airport-247791/

Cross Platform C++ Development without CMake

Summary

This is a technique that I use for small tools (command line as well as GUI) that I build which need to work across the common desktop environments: Windows, macOS, and Linux. It keeps the build system simple and easy to comprehend.


There are many ways to develop cross-platform C++ applications. One of the most popular ways is to use CMake. However, CMake can be difficult to use and understand, especially for beginners. Most developers are more familiar with the native build systems on the operating system they work on: Make on Linux, Visual Studio/MSBuild on Windows, and Xcode on macOS. Furthermore CMake may be a bit of an overkill for small projects.

For many of the small C++ projects that I work on, I’ve developed a technique to use the native build systems of the respective plafroms to build my application. This allows me to use tools that I am already familiar with on those platforms.

The idea is to create each build system in a separate directory and then 'merge' them so that they reside in the same directory hierarchy and operate on the same source files.

I’ll describe how to do this manually at first and then point to a simple Python script that automates this task. We will start with a simple C++ application that prints the greatest common divisor of two numbers.? We will then create ?a build system for each operating system and merge them into a single directory hierarchy.

First we create the directory hierarchy for our project, We'll call the root gcd. Under the root we will place all our source under another directory named gcd. This should match the name of the project you create in Visual Studio and Xcode. Now check this code into your version control system.

On a Windows machine, open VisualStudio and create a new project called gcd (in a different location.) You can copy over the source for your project from the directory you created earlier. Now build your project and make sure it works.

On a macOS machine, open Xcode and create a new project called gcd (in a different location.) You can copy over the ?source for your project from the directory you created earlier. Now build your project and make sure it works.

Finally, on a Linux machine, create a Makefile for your project in the root directory. Have the build output go to a directory called ‘build’ located in the root folder. The exact name of the output directory doesn't matter, as long as it is different from the source directory and doesn't class with the other build systems. Build your code and make sure it works on Linux.?

Next, copy over all the build artifacts from Windows, macOS, and Linux to one location. Then place the .sln file from the Windows system under the root directory. Place the gcd.xcodeproj directory in the same location. After that, place the Makefile in the root directory as well.

Then place gcd.vcxproj and gcd.vcxproj.filters in the source directory. The final directory structure shoudl look as follows:

Final Directory Structure for a Cross-Platform C++ Application


Copy this directory structure to the Windows, macOS, and Linux machines. You can do that by checking in the directory structure into your version control system and then checking out the code on the respective machines. (That is what I do.) ?Finally make sure you can build and run the application on all three platforms.

This technique allows you to build cross-platform C++ applications without the complexity of a non-native build system like CMake. It works reasonably well for small projects but may not scale to larger projects.

Limitations

The key limitation with this way of managing a cross-platform project is that project dependencies and build setting are duplicated across three different build systems. Every new source file added or deleted must be added or deleted from all three build systems. This can be error-prone and time-consuming. The same thing will need to be done with compiler and linker flags, and link libraries etc. If you accidentally diverge you will not really discover the issue easily. This can be a huge problem for debugging.

Automating Project Creation

For small projects, like the AR based git explorer that I'm working on, I use a simple python script. This program copies the project files of different platforms into a single directory into their correct locations

You can find the script in my public GitHub repository. https://github.com/sumanthvepa/experiments/tree/main/python/cppproject

要查看或添加评论,请登录

Sumanth Vepa的更多文章

社区洞察

其他会员也浏览了