Compiling source files


// main.cpp
#include <iostream>
int main() {
    std::cout << "Hello, CMake!" << std::endl;
    return 0;
}

// CMakeLists.txt
cmake_minimum_required(VERSION 3.10) #CMake version
project(MyProject)      #project-name
add_executable(MyExecutable main.cpp)   #(exe_name generate_from_main.cpp)

$ mkdir build
$ cd build
build$ cmake ..
build$ ls
cmake_install.cmake  Makefile  CMakeFiles  CMakeCache.txt

build$ make
[ 50%] Building CXX object CMakeFiles/MyExecutable.dir/main.cpp.o
[100%] Linking CXX executable MyExecutable
[100%] Built target MyExecutable

build$ ls
CMakeCache.txt  CMakeFiles  Makefile  MyExecutable  cmake_install.cmake
$ ./MyExecutable
      

Files Generated

Once (cmake ..) is run following files are generated
1. cmake_install.cmake:
instructions for installing built targets to their final locations
when we run 'make install' or cmake --install


          
2. Makefile:
The actual build script generated for your chosen platform

          
3. CMakeFiles directory:
Contains metadata, intermediate files and auxiliary scripts needed for the build system to operate.

$ ls CMakeFiles/
3.22.1                           CMakeOutput.log  Makefile.cmake  MyExecutable.dir       cmake.check_cache
CMakeDirectoryInformation.cmake  CMakeTmp         Makefile2       TargetDirectories.txt  progress.marks
          
4.CMakeCache.txt
Persistent Cache and stores key-value pairs for internal and
user-defined variables such as compiler paths, options, and feature flags

CMAKE_C_COMPILER:FILEPATH=/usr/bin/cc
CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++
//Flags used by the CXX compiler during MINSIZEREL builds.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG