From the course: Advanced C++: Building Projects with CMake

Getting familiar with CMake

- [Instructor] Now that you have CMake installed and accessible from the command line, let's get comfortable with some basic concepts. First, I'd like to clarify that CMake isn't a compiler, it's a build system generator. That means it takes a high-level description of your project, written in a special file called CMakeLists.txt, and generates the appropriate build files for your platform. That could be Make files, Ninja build scripts, Visual Studio solutions, or Xcode projects. Once those files are generated, you can compile your project using a tool like Make, Ninja, or just run cmake minus minus build. Let's look at a simple example. Suppose we have the following files. The main.cpp contains a simple program that prints Hello from CMake to the terminal. And here's the CMakeLists.txt file. This tells CMake the following. We require at least CMake 3.10. We're creating a project called Hello CMake with version 1.0 and we want to build an executable called Hello from the main.cpp file. To configure and build the project, we use CMake standard out of source workflow. So I'll open the built-in terminal and first let's create a separate folder to hold our build files. And let's switch to it. Next, execute cmake dot dot. This runs CMake, pointing it to the directory with our CMakeLists.txt. After running this command, the build folder is populated with various files and folders. Notice the generated Makefile. We'll discuss these files later. But for now, let's continue with the final command. Cmake minus minus build dot. This will build the project using the generated files and it should produce an executable called Hello in the build folder. If everything's set up correctly, we can now run it. Awesome. So that's CMake in action from source code to executable in just a few steps.

Contents