C++ - CMake - Setup with Cygwin

There are many ways to create a Makefile.

In this tutorial we are going to see how to generate a Makefile with CMake on Windows.

Indeed, CMake is a complex but interesting tool that can generate native Makefiles for the compiler of your choice.

But it's not all, CMake can also create some useful project files, for a lot of IDE (such as Visual Studio or Eclipse for example) and almost every OS (like GNU/Linux, Windows, Mac, etc.).

In order to have a nice terminal, I'll use the Cygwin CLI  during this CMake tutorial.

Let's get started.

First of all

In the GNU world we have the GNU Build System with the famous two-step process: configure and make, one calls this the Autotools.

  1. The configure step will scan the system information to create a Makefile appropriate for the current build environment. 
  2. Then the make tool will use this Makefile to generate an executable.

The CMake tool propose to help us configuring our workspace like the Autotools but this time with the two-step process: cmake and make.

  1. The cmake command will scan the system information to create the Makefile
  2. The make tool will generate the executable.

Thus it reduces our mechanism to only 2 steps.

It's quite interesting.

So why creating a new tool as the GNU one works well?

To be honest the main reason is because in the Windows world there is no native GNU tools available.

That's why CMake has been created, it works on every operating system (the first letter "C" standing for Cross-platform).

Furthermore, with CMake it will be possible to generate Visual Studio project files, like .sln and .vcxproj for example.

Cygwin

Our main CLI for this tutorial will be Cygwin:

We will install it in the following directory:

  • C:\soft\cygwin

If you don't know what to install, just follow this Cygwin tutorial (Part 1 and 2).

CMake is already included inside the packages you downloaded if you followed this Cygwin tutorial.

So no need to download it from the official website.

Be careful though if you installed CMake as an independant program because with Cygwin you won't use the one downloaded from the official website.

Indeed the cmake command (from Cygwin) calls the cmake.exe file within the following directory:

C:\soft\cygwin\bin

And not the one you've downloaded on the official website.

Anyway, if you want to know which version you are using, type the following command in your favorite CLI:

cmake -version

From the Cygwin terminal, you should see something like this:

> cmake -version
cmake version 3.6.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

CMake

We will use the following directory as main directory for our tutorial:

  • C:\dev\cmake\tutorial

To start our CMake tutorial, we need 3 files that we have to create ourselves:

  • CMakeLists.txt
  • Badprog.cpp
  • Badprog.h

So we have these 3 files now in C:\dev\cmake\tutorial:

├── CMakeLists.txt
├── Badprog.cpp
└── Badprog.h

CMakeLists.txt

This file is a simple text file with the following code.

# Badprog.com

# Minimum version
cmake_minimum_required(VERSION 3.6)

# add the executable
add_executable(BadprogTutorial Badprog.cpp)

With the cmake_minimum_required command, we tell CMake to generate an error if the CMake version is lower than the one required (3.6 in our case).

The add_executable() command is to generate the project executable file.

It takes 2 arguments, the first is the name of the executable and the second is the name of source files needed to generate this executable.

It's of course possible to add more than 1 file for this second parameter, but here we have only 1 file.

Badprog.cpp

Straightforward file, it prints a string from the code.

// Badprog.com

#include <iostream>
#include "Badprog.h"

using namespace std;

/**
* Constructor
*/
Badprog::Badprog() {
    cout << "Hello from Badprog and CMake! :D" << endl;
}

/**
* Main
*/
int main(int argc, char *argv[])
{
    Badprog bapdrog;
    return 0;
}

Badprog.h

Only the constructor to have a really easy example.

// Badprog.com

#ifndef _BADPROG_H
#define _BADPROG_H

class Badprog
{
public:
    Badprog();
};

#endif // !_BADPROG_H

Generating the Makefile

The first command to type in Cygwin is:

cmake CMakeLists.txt

Done, our Makefile has been created.

But it comes with many other different files, below the complete list:

├── Badprog.cpp
├── Badprog.h
├── cmake_install.cmake
├── CMakeCache.txt
├── CMakeFiles
│   ├── 3.6.2
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeRCCompiler.cmake
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.exe
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │       ├── a.exe
│   │       └── CMakeCXXCompilerId.cpp
│   ├── BadprogTutorial.dir
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── depend.make
│   │   ├── DependInfo.cmake
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── feature_tests.bin
│   ├── feature_tests.c
│   ├── feature_tests.cxx
│   ├── Makefile.cmake
│   ├── Makefile2
│   ├── progress.marks
│   └── TargetDirectories.txt
├── CMakeLists.txt
└── Makefile

All those files aren't useful for us, they are used by CMake.

Using the Makefile

Now let's use our Makefile by simply typing in Cygwin:

make

A new message should appear in your terminal:

Scanning dependencies of target BadprogTutorial
[ 50%] Building CXX object CMakeFiles/BadprogTutorial.dir/Badprog.cpp.o
[100%] Linking CXX executable BadprogTutorial.exe
[100%] Built target BadprogTutorial

Here we are, the executable BadprogTutorial.exe has been added.

You can launch it by typing:

./BadprogTutorial.exe

The message from the code should appear on Cygwin:

Hello from Badprog and CMake! :D

Conclusion

This CMake tutorial was an Hello World tutorial.

Simple to understand, I least I hope.

Congratulations if you read until here, you did it! laugh

Comments

Comment: 

"To be honest the main reason is because in the Windows world there is no native GNU tools available."

of course there is one ! I use it since almost 20 years !

Look at msys2 homepage for more informations

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.