Как установить cmake на windows

How to install CMake on Windows


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.


Dependencies

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!


Installation

01. Download CMake by clicking the button below:

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.


02. Setup presentation

Just click on Next.

01


03. Accept the license terms

And then click Next.

02


04. Add CMake to your 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.

03


05. Select installation path

Hint: Keep what already appears and click Next.

04


06. Start the installation

Click on Install and wait for the installation to finish.

05

Installing:

06

Once finished, click on Finish:

07


Testing CMake

Open PowerShell or CMD and type the command:

cmake --version

If the output is similar to the image below, everything is right and installed perfectly:

08


Compiling with CMake

  • 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:

cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
add_executable(hello main.cpp)

09

  • Also create a main.cpp file with a basic code, example:
#include <iostream>

auto main(int argc, char** argv) -> int {
   std::printf("CMake is working on Windows!\n");
   return EXIT_SUCCESS;
}

12

At the end your MyProject folder will have 2 files: CMakeLists.txt and main.cpp as shown in the image below:

10

  • Now run the command:
cmake -G "Unix Makefiles" -B build

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:

11

  • Once finished, enter the build folder (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:

13

That’s it, now you can compile your C and C++ code with CMake!


Useful links

  • https://cmake.org/
  • https://cmake.org/download/
  • https://github.com/Kitware/CMake/

cmake

windows



Related articles



This article explains how CMake handles complex build processes. Earthly provides robust caching mechanisms to improve on CMake’s efficiency. Learn more about Earthly.

CMake is an open source multiplatform tool that you can use to help with build automation, testing, packaging, and the installation of software. One of the main advantages of CMake is that it supports generating build scripts for a wide variety of platforms including Windows, macOS, and Linux. This gives developers the flexibility they need when building a new project and hopefully helps speed up the build.

In this article, you’ll learn more about CMake and if it’s the right solution for you. You’ll also walk through the process of installing it on Windows.

Pros and Cons of CMake

Pros & Cons**

There are a number of benefits to using CMake to help build your solutions, including the fact that CMake can run on all major platforms, including Windows, Linux, and macOS. This means that a team of developers who are using different operating systems can still have common development tools.

Using CMake for projects, instead of something like Visual Studio projects, helps you avoid being locked into one integrated development environment (IDE). A team can create a project using CMake, and then each developer can use their preferred IDE to work on it. CMake integration is supported by a variety of IDE tools, including CLion, Atom, and Visual Studio.

It may be useful for some developers and software architects to understand what dependencies are within their projects and the nature of the dependency. CMake can create a visualization of any dependencies within the project, that you can use to create documentation.

While many would assume that CMake’s active development is an advantage (which it is!), it also has its downsides: Not everyone within a developer team will be running the same version of CMake. However, to help reduce this issue, CMake has introduced policies that define how certain CMake features should behave. This means your team can set a common policy regardless of what version people are using.

Another disadvantage of CMake is the lack of information available for some versions. It can be hard to find accurate information to resolve an issue or answer a question.

When to Use CMake

As you can see, CMake can be a useful build system. Developers using CMake can quickly check performance on different build backends and easily move between them.

CMake can also help to keep your source code folder clean. It can separate the build system, intermediaries, and output files from the source code, keeping your code clean for the future.

Install CMake on Windows

There are several ways to install CMake on Windows. The first option is to use pre-compiled binaries as either the Windows Installer file (MSI) or the compressed version (ZIP) files. You just have to download the files and then walk through the interactive installation wizard.

The second option is to download and build CMake from the source, and CMake even includes a source release download option.

The third option is to use a package manager tool, such as Windows Package Manager or Chocolatey. This can be especially convenient if you already have either installed on your machine. We will use the package manager method you’ll use for your installation here.

If you’re using Windows Package Manager, run the following command to install CMake:

winget install kitware.cmake

If you’re using Chocolatey, use the following command:

Install a CMake Extension with Visual Studio

Once you’ve finished installing CMake on Windows, you need to install an extension that lets you integrate the functionality of CMake into your IDE (Visual Studio is used here).

To install the CMake extension, you need to select Start and then open the Visual Studio Installer from the menu:

Visual Studio Installer

Once the Visual Studio Installer loads, select Modify:

Modify your Visual Studio Installer

Under Workloads, locate Desktop development with C++, which can be found under Desktop & Mobile:

Desktop development with C++ component

On this same page, you also need to find and select the Linux and embedded development with C++ component. This component ensures you have cross-platform CMake development capabilities:

Linux development with C++ component

Once you’ve selected both, select Modify again, and the additional tools will be installed.

Integrate an IDE

Now that you’ve installed CMake and a few components, it’s time to use them. Start a new project by opening Visual Studio and selecting Create a new project > CMake project:

New project selection

After selecting CMake project, Visual Studio, and CMake will create a directory and a file called CMakeLists.txt, which is a set of instructions describing certain settings within the project, such as source files and targets.

When Visual Studio detects that there is a CMakeLists.txt file within a project that’s been opened, it adds CMake items to the Project menu, which gives you access to commands for viewing and editing CMake scripts:

Project menu

Configure CMake Projects

CMake supports two files that let you specify build and test options. These files are CMakePresets.json and CMakeUserPresets.json. Microsoft recommends that you use CMakePresets.json.

To ensure that Visual Studio uses the CMakePresets.json file, you need to enable it. You can do so via Tools > Options > CMake > General. Please ensure that the Always use CMake Presets option is selected:

Options menu settings

If Visual Studio can’t find a CMakePresets.json file, it will fall back to using the default configure presets.

There are three key settings within your CMakePresets.json file: target systems, configure presets, and build presets. The target system is the system on which CMake is invoked to configure and build your project. You can use systems installed on your local machine, SSH connections, and all Windows Subsystem for Linux (WSL) installations.

The configure preset option is used when CMake is invoked to generate the project build system. Depending on your target system, the options for this will change. So if you are using Ubuntu as your target system, your configure preset could be Linux debug.

The build preset value is used when CMake is invoked to build the project and should align with the configure preset. If Visual Studio can’t find any build presets that are associated with the configure preset, then it will provide the default build preset.

As you can see, there are a lot of options available to configure your CMake projects to suit your needs and project. If you’re looking for even more information on how to configure your projects, check out this Visual Studio documentation.

Build CMake Projects on Visual Studio

When you’re ready, Visual Studio gives you several options to build your CMake projects, including the toolbar or the Solution Explorer.

If you want to use the toolbar, you need to find the Startup Item drop-down:

Startup Item drop-down

Select your preferred target to build, and either press F5 or choose Run.

If you want to use the Solution Explorer, navigate to the Solution Explorer on the right-hand side of your screen. By default, it will be set to the Folder View:

Solution Explorer — Folder View

You need to change the Folder View to the CMake Targets View. To do this, select the view icon:

View icon

Then select CMake Targets View:

CMake Targets View

Then right-click on CMake Target and select Build from the context menu:

Build

Lastly, you can select Build > Build All from the main menu of Visual Studio to build your CMake project:

Build All

After completing either one of these options, you can find the build results within the Output window and Error List:

Output window

Debug CMake Projects

As you know, when writing code, it’s never perfect the first time, which is why you’ll need to be able to debug your code.

To debug your CMake project using Visual Studio, start by selecting one of the targets shown in the Startup Item drop-down within the toolbar:

Startup Item drop-down

Then select Debug > Start Debugging from the main toolbar:

Debug

If any changes have been made since the last time you built the project, the debug command will build it first before the debugging can take place.

The launch.vs.json file can be used to customize your CMake debugging session. This file can be used to input any environment variables or command line arguments that you might want to pass into the program during debugging.

Conclusion

CMake is a versatile tool that aids in build automation, testing, packaging, and software installation. It integrates with many IDEs, giving developers the flexibility to stick with their preferred IDE. It ensures your application’s compatibility across any platform. While CMake isn’t for everyone, it can be a robust solution for the right developer or project.

And if you’re looking to optimize your builds further, you might want to give Earthly a shot for its efficient caching and straightforward syntax.

Earthly Lunar: Monitoring for your SDLC
Achieve Engineering Excellence with universal SDLC monitoring that works with every tech stack, microservice, and CI pipeline.

Get a Demo

I am working on a project which require me to build and compile MariaDB database. To build MariaDB, CMake is required. CMake will first verify on which operating system, it is being run, then it will create and\or copy the appropriate directories, files on the operating system.

CMake

It is an open source software under the BSD-3-Clause license. C++ compiler is required to run CMake build system. CMake is used in the software development industry for build automation, testing, packaging and installation of software.

CMake is not a build system, it creates the build system files for other operating systems, which includes support for directory hierarchy.

Let’s install CMake on windows.

Download the code

https://cmake.org/download/

Choose executable from the list
At this time, the current version is 3.26.0

https://github.com/Kitware/CMake/releases/download/v3.26.0/cmake-3.26.0-windows-x86_64.msi

Install CMake from the executable

Step 1 Double click on the downloaded executable file

Step 2 Follow the instructions on the screen

cmake_installation_windows_step_1

cmake_installation_windows_step_2

Alert : Make sure to add CMake to the PATH

cmake_installation_windows_step_3

cmake_installation_windows_step_4

cmake_installation_windows_step_5

cmake_installation_windows_step_6

cmake_installation_windows_step_7

Step 2 Make sure CMake is installed on the system

Reboot your Machine.

# Command 
where cmake
# Output
C:\Program Files\CMake\bin\cmake.exe

Enter fullscreen mode

Exit fullscreen mode

CMake is properly installed on your system.

Disclaimer: I have been working on a project which require me to install, configure, troubleshoot various packages on multiple Operating system. If you find any issue, please reach out to me.

Contents

Installing CMake#

Tip

Your CMake version should be newer than your compiler. It should be newer than the libraries you are using. New versions work better for everyone.

If you have a built in copy of CMake, it isn’t special or customized for your system. You can easily install a new one instead, either on the system level or the user level. Feel free to instruct your users here if they complain about a CMake requirement being set too high. Especially if they want 3.15+ support. Maybe even if they want 4.0+ support…

Quick list (more info on each method below)#

Ordered by author preference:

  • All

    • Pip(x) (official, often updates same-day)

    • Anaconda / Conda-Forge

  • Windows

    • Winget

    • Chocolatey

    • Scoop

    • MSYS2

    • Download binary (official)

  • MacOS

    • Homebrew

    • MacPorts

    • Download binary (official)

  • Linux

    • Snapcraft (official)

    • APT repository (Ubuntu/Debian only) (official)

    • Download binary (official)

Official package#

You can download CMake from KitWare. This is how you will probably get CMake if you are on Windows. It’s not a bad way to get it on macOS either (and a Universal2 version is supplied supporting both Intel and Apple Silicon), but using brew install cmake is much nicer if you use Homebrew (and you should; Apple even supports Homebrew such as during the Apple Silicon rollout). You can also get it on most other package managers, such as Chocolatey for Windows or MacPorts for macOS.

On Linux, there are several options. Kitware provides a Debian/Ubuntu apt repository, as well as snap packages. There are universal Linux binaries provided, but you’ll need to pick an install location. If you already use ~/.local for user-space packages, the following single line command[1] will get CMake for you [2]:

~ $ wget -qO- "https://cmake.org/files/v4.0/cmake-4.0.0-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C ~/.local

The names changed in 3.20; older releases had names like cmake-3.19.7-Linux-x86_64.tar.gz. If you just want a local folder with CMake only:

~ $ mkdir -p cmake-4.0 && wget -qO- "https://cmake.org/files/v4.0/cmake-4.0.0-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C cmake-4.0
~ $ export PATH=`pwd`/cmake-4.0/bin:$PATH

You’ll obviously want to append to the PATH every time you start a new terminal, or add it to your .bashrc or to an LMod system.

And, if you want a system install, install to /usr/local; this is an excellent choice in a Docker container, for example on GitLab CI. Do not try it on a non-containerized system.

docker $ wget -qO- "https://cmake.org/files/v4.0/cmake-4.0.0-linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C /usr/local

If you are on a system without wget, replace wget -qO- with curl -s.

You can also build CMake on any system, it’s pretty easy, but binaries are faster.

CMake Default Versions#

Here are some common build environments and the CMake version you’ll find on them. Feel free to install CMake yourself, it’s 1-2 lines and there’s nothing “special” about the built in version. It’s also very backward compatible.

Windows#

The winget package is a good way to get CMake. Other options:

Also Scoop is generally up to date. The normal installers from CMake.org are common on Windows, too.

MacOS#

Homebrew is quite a bit more popular nowadays on macOS, at least according to Google Trends.

Linux#

RHEL/CentOS#

The default on 8 is not too bad, but you should not use the default on the end-of-life CentOS 7. Use the EPEL package instead.

Ubuntu#

Debian#

Other#

General tools#

Just pip install cmake on many systems. Add --user if you have to (modern pip does this for you if needed). This does not supply Universal2 wheels yet.

CI#

If you are using GitHub Actions, also see the jwlawson/actions-setup-cmake action, which can install your selection of CMake, even in a docker action run.

Full list#

Versions less than 3.15 are marked by a deeper color of red.

Full listing

Also see pkgs.org/download/cmake.

Pip#

This is also provided as an official package, maintained by the authors of CMake at KitWare and several PyPA members, including myself. It’s now supported on special architectures, like PowerPC on Linux and Apple Silicon on macOS, and on MUSL systems like Alpine too. If you have pip (Python’s package installer), you can do:

And as long as a binary exists for your system, you’ll be up-and-running almost immediately. If a binary doesn’t exist, it will try to use KitWare’s scikit-build package to build, and will require an older copy of CMake to build. So only use this system if binaries exist, which is most of the time.

This has the benefit of respecting your current virtual environment, as well. It really shines when placed in a pyproject.toml file, however — it will only be installed to build your package, and will not remain afterwards! Fantastic.

This also, of course, works with pipx. So you can even use pipx run cmake to run CMake in a disposable virtual environment, without any setup — and this works out-of-the-box on GitHub Actions, since pipx is a supported package manager there!

Tip

Personally, on Linux, I put versions of CMake in folders, like /opt/cmake312 or ~/opt/cmake312, and then add them to LMod. See envmodule_setup for help setting up an LMod system on macOS or Linux. It takes a bit to learn, but is a great way to manage package and compiler versions.


To set up CMake on Windows, you can follow these general steps:

  1. Download the CMake installer from the official website.
  2. Run the installer and follow the on-screen instructions to install CMake on your computer.
  3. Once the installation is complete, open a command prompt and type cmake —version to verify that CMake has been successfully installed.
  4. To use CMake in your projects, create a CMakeLists.txt file in the root directory of your project and define your project configuration settings in this file.
  5. Generate build files for your project by running cmake . in the command prompt from the directory where your CMakeLists.txt file is located.
  6. Use the generated build files to compile and build your project using a build tool like Visual Studio or MinGW.
  7. You can also set up a build directory to separate your source code from the build files by creating a new directory and running cmake path/to/source/code from that directory.
  8. Modify the CMakeLists.txt file as needed to add dependencies, specify compiler flags, or configure other project settings.
  9. Build your project by running the build tool (e.g. Visual Studio) using the generated build files.

Best Software Engineering Books of November 2024




1


Rating is 5 out of 5

Introduction to Engineering Design: Engineering Skills and Rover Missions




2


Rating is 4.9 out of 5

Fundamentals of Software Architecture: An Engineering Approach




3


Rating is 4.8 out of 5

Code Complete: A Practical Handbook of Software Construction, Second Edition

  • Microsoft Press




4


Rating is 4.7 out of 5

The Essentials of Modern Software Engineering: Free the Practices from the Method Prisons! (ACM Books)




5


Rating is 4.6 out of 5

Software Engineering




6


Rating is 4.5 out of 5

Clean Code: A Handbook of Agile Software Craftsmanship

  • Prentice Hall




7


Rating is 4.4 out of 5

Clean Architecture: A Craftsman’s Guide to Software Structure and Design (Robert C. Martin Series)




8


Rating is 4.3 out of 5

Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software 2nd Edition




9


Rating is 4.2 out of 5

Software Engineering: A Practitioner’s Approach

  • McGraw-Hill Science Engineering Math




10


Rating is 4.1 out of 5

ISE SOFTWARE ENGINEERING: A PRACTITIONERS APPROACH




11


Rating is 4 out of 5

Essentials of Software Engineering

What is CMake build directory on Windows?

The CMake build directory on Windows is the location where CMake generates the build system files (such as Makefiles or Visual Studio project files) and where the project is built by the selected build system. By default, CMake creates a directory called «build» in the root directory of the project to store these build files and build artifacts. However, the build directory can be customized using the CMake configuration options.

What is CMake generator in Windows?

CMake generator in Windows is a tool that allows CMake to generate native build configurations for different operating systems and build environments. It determines the type of project files that CMake will generate, such as Visual Studio solutions, makefiles, or Xcode projects. The generator specifies the build system to use when building the software. Some common CMake generators for Windows include Visual Studio, Ninja, and MinGW Makefiles.

How to specify output directories in CMake on Windows?

To specify output directories in CMake on Windows, you can use the SET command to define the output directory for the different types of build outputs, such as libraries, executables, and object files. Here’s an example of how you can specify output directories in CMake on Windows:

1
2
3
4
5
6
7
8
# Set the output directory for libraries
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# Set the output directory for executables
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Set the output directory for object files
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/obj)

You can customize the output directories based on your project’s structure and requirements by modifying the paths in the SET commands. Make sure to add this code to your CMakeLists.txt file before any other project settings or targets are defined. After adding these commands, CMake will generate project files that output the build results to the specified directories on Windows.

How to create CMakeLists.txt file in Windows?

To create a CMakeLists.txt file in Windows, you can follow these steps:

  1. Open a text editor such as Notepad or Visual Studio Code.
  2. Start by defining the minimum required version of CMake at the top of the file. For example:
    cmake_minimum_required(VERSION 3.10)
  3. Next, add the project name using the project() command followed by the project name. For example:
    project(MyProject)
  4. Add any additional configuration settings or options for your project. This can include setting variables, including directories, enabling/disabling features, etc.
  5. Specify the source files and dependencies for your project using the add_executable() or add_library() commands. For example:
    add_executable(MyExecutable main.cpp)
  6. If your project requires external libraries or packages, you can use the find_package() command to locate and include them.
  7. Lastly, specify any additional build steps or configuration settings such as compiler flags, include directories, etc.
  8. Save the file with the name “CMakeLists.txt” in the root directory of your project.

Once you have created the CMakeLists.txt file, you can run CMake to generate build files for your project. This can be done using the CMake GUI or by running the cmake command in the terminal.

What is CMake binary directory on Windows?

The CMake binary directory on Windows is the directory where the compiled binary files of a CMake project are stored. By default, CMake places the binary files in a subdirectory called «build» in the root directory of the CMake project. The binary directory is where the executables, libraries, and other output files generated by CMake during the build process are located.

How to add include directories in CMake on Windows?

To add include directories in CMake on Windows, you can use the include_directories command in your CMakeLists.txt file.
Here’s an example of how you can add include directories to your CMake project:

1
2
3
4
5
6
# Specify the include directories
include_directories(
  ${CMAKE_SOURCE_DIR}/include
  ${CMAKE_SOURCE_DIR}/external
  ${CMAKE_SOURCE_DIR}/thirdparty
)

In this example, we have specified three include directories: include, external, and thirdparty. You can add as many include directories as needed by adding additional lines to the include_directories command.

Make sure to replace ${CMAKE_SOURCE_DIR} with the actual path to your project’s source directory.

After adding the include directories, you can use the target_include_directories command to specify additional include directories for specific targets, if needed:

1
2
# Specify include directories for a specific target
target_include_directories(my_target PUBLIC ${CMAKE_SOURCE_DIR}/my_library)

This will add the my_library directory to the include directories for the my_target target.

Remember to re-run CMake to regenerate the build files after making changes to the CMakeLists.txt file.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Доступно необязательное исправление windows 10 что это
  • Rdp настройка безопасности windows
  • Поддержка windows server 2016 когда заканчивается
  • Динамическая маршрутизация в windows
  • Windows 10 home 1809