CMake is a system to automate compilation tasks of code files created in C and C++.
On Unix type systems we can say that it is a “Makefile” generator. He is classified as a SCons.
We already made a brief introduction here on the blog about CMake, but it is also possible to generate Makefiles in Windows, although CMake also generates files for Visual Studio.
Today we will see how to install CMake on Windows.
As in this example we will use GCC, it is important that you have it installed on your Windows via MinGW, see here how to do it.
Now let’s go step by step!
Download CMake for Windows 64-bit
After downloading, double-click on the downloaded file: cmake-3.26.0-rc6-windows-x86_64.msi
and give administrator permissions to start the installation setup.
Just click on
Next
.
And then click
Next
.
PATH
Check the Add CMake to the system PATH for all users
option for the command to be available to all system users. And then click on Next
.
Hint: Keep what already appears and click
Next
.
Click on Install
and wait for the installation to finish.
Open PowerShell or CMD
and type the command:
If the output is similar to the image below, everything is right and installed perfectly:
Create a folder, for example MyProject
, enter it (by CMD or PowerShell, cd MyProject
) and inside it do:
Create a file named CMakeLists.txt
with your Preferred Code Editor or IDE and fill it with the code below:
main.cpp
file with a basic code, example:At the end your MyProject
folder will have 2 files: CMakeLists.txt
and main.cpp
as shown in the image below:
It will create a folder called
build
inside your project. If you don’t say-G "Unix Makefiles"
it will prepare files for Visual Studio, in which case we want a Unix-like Makefile.
The output will be similar to the image below:
cd build
) and run the make
command (if this command does not exist, it is because you did not rename the MinGW file to that name as mentioned in the article about MinGW cited above, then most likely the command will be mingw32-make
). And then run the final binary: hello
:That’s it, now you can compile your C and C++ code with CMake!