# How to Build a CMake-Based Project

DevFeed: [How to Build a CMake-Based Project](<https://devfeed.tech/articles/how-to-build-a-cmake-based-project-21008.md>)

Original publisher: [Read original article](<https://preshing.com/20170511/how-to-build-a-cmake-based-project>)

Author: Jeff Preshing

Published: 2017-05-11T12:30:00Z

Content type: tutorial

Language: en

Sources: [Jeff Preshing](<https://devfeed.tech/sources/jeff-preshing.md>)

Topics: [CMake](<https://devfeed.tech/topics/cmake.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Linux](<https://devfeed.tech/topics/linux.md>), [macOS](<https://devfeed.tech/topics/macos.md>), [Windows](<https://devfeed.tech/topics/windows.md>), [Caching](<https://devfeed.tech/topics/caching.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>), [make](<https://devfeed.tech/topics/make.md>), [Visual Studio](<https://devfeed.tech/topics/visual-studio.md>), [Xcode](<https://devfeed.tech/topics/xcode.md>), [OpenGL](<https://devfeed.tech/topics/opengl.md>)

Tags: [build](<https://devfeed.tech/tags/build.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [cache](<https://devfeed.tech/tags/cache.md>), [cmake](<https://devfeed.tech/tags/cmake.md>), [command-line](<https://devfeed.tech/tags/command-line.md>), [gui](<https://devfeed.tech/tags/gui.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [linux](<https://devfeed.tech/tags/linux.md>), [macos](<https://devfeed.tech/tags/macos.md>), [visual-studio](<https://devfeed.tech/tags/visual-studio.md>), [windows](<https://devfeed.tech/tags/windows.md>), [xcode](<https://devfeed.tech/tags/xcode.md>)

## AI overview

A practical guide to configuring and building CMake-based C/C++ projects across Windows, macOS, and Linux. It explains source and binary folders, generated build pipelines, CMake's cache, and several command-line and graphical workflows.

## Source excerpt

CMake is a versatile tool that helps you build C/C++ projects on just about any platform you can think of. It's used by many popular open source projects including LLVM, Qt, KDE and Blender. All CMake-based projects contain a script named CMakeLists.txt, and this post is meant as a guide for configuring and building such projects. This post won't show you how to write a CMake script - that's getting ahead of things, in my opinion. As an example, I've prepared a CMake-based project that uses SDL2 and OpenGL to render a spinning 3D logo. You can build it on Windows, MacOS or Linux. The information here applies to any CMake-based project, so feel free to skip ahead to any section. However, I recommend reading the first two sections first. The Source and Binary Folders The Configure and Generate Steps Running CMake from the Command Line Running cmake-gui Running ccmake Building with Unix Makefiles Building with Visual Studio Building with Xcode Building with Qt Creator Other CMake Features If you don't have CMake yet, there are installers and binary distributions on the CMake website. In Unix-like environments, including Linux, it's usually available through the system package manager. You can also install it through MacPorts, Homebrew, Cygwin or MSYS2. The Source and Binary Folders CMake generates build pipelines. A build pipeline might be a Visual Studio .sln file, an Xcode .xcodeproj or a Unix-style Makefile. It can also take several other forms. To generate a build pipeline, CMake needs to know the source and binary folders. The source folder is the one containing CMakeLists.txt. The binary folder is where CMake generates the build pipeline. You can create the binary folder anywhere you want. A common practice is to create a subdirectory build beneath CMakeLists.txt. By keeping the binary folder separate from the source, you can delete the binary folder at any time to get back to a clean slate. You can even create several binary folders, side-by-side, that use diffe