Enable Verbose Output in Makefile Builds using CMake

Enable Verbose Output in Makefile Builds using CMake

When working with CMake, the default output during a build is often quite minimal. While this is fine for simple development, it can make debugging tricky if you want to understand exactly what compiler commands are being run under the hood. This tutorial shows how to enable verbose output in Makefile builds using CMake.

CMake provides a variable called CMAKE_VERBOSE_MAKEFILE that controls whether detailed build commands are printed during the build process. Keep in mind that this variable only works with the Unix Makefile and Ninja generators. Here's a minimal example CMakeLists.txt that enables verbose build output:

cmake_minimum_required(VERSION 3.27)
project(myapp)
 
set(CMAKE_CXX_STANDARD 17)
 
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON" FORCE)
 
add_executable(${PROJECT_NAME} main.cpp)

Before starting the build, run the following command to generate the necessary build files:

cmake -S . -B build

Now run the command to build the project:

cmake --build build

This will produce verbose output like the following:

Change Dir: '/home/ubuntu/myproject/build'

Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile
/usr/bin/cmake -S/home/ubuntu/myproject -B/home/ubuntu/myproject/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ubuntu/myproject/build/CMakeFiles /home/ubuntu/myproject/build//CMakeFiles/progress.marks
/usr/bin/gmake  -f CMakeFiles/Makefile2 all
gmake[1]: Entering directory '/home/ubuntu/myproject/build'
/usr/bin/gmake  -f CMakeFiles/myapp.dir/build.make CMakeFiles/myapp.dir/depend
gmake[2]: Entering directory '/home/ubuntu/myproject/build'
cd /home/ubuntu/myproject/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ubuntu/myproject /home/ubuntu/myproject /home/ubuntu/myproject/build /home/ubuntu/myproject/build /home/ubuntu/myproject/build/CMakeFiles/myapp.dir/DependInfo.cmake "--color="
gmake[2]: Leaving directory '/home/ubuntu/myproject/build'
/usr/bin/gmake  -f CMakeFiles/myapp.dir/build.make CMakeFiles/myapp.dir/build
gmake[2]: Entering directory '/home/ubuntu/myproject/build'
[ 50%] Building CXX object CMakeFiles/myapp.dir/main.cpp.o
/usr/bin/c++    -MD -MT CMakeFiles/myapp.dir/main.cpp.o -MF CMakeFiles/myapp.dir/main.cpp.o.d -o CMakeFiles/myapp.dir/main.cpp.o -c /home/ubuntu/myproject/main.cpp
[100%] Linking CXX executable myapp
/usr/bin/cmake -E cmake_link_script CMakeFiles/myapp.dir/link.txt --verbose=1
/usr/bin/c++ -Wl,--dependency-file=CMakeFiles/myapp.dir/link.d CMakeFiles/myapp.dir/main.cpp.o -o myapp
gmake[2]: Leaving directory '/home/ubuntu/myproject/build'
[100%] Built target myapp
gmake[1]: Leaving directory '/home/ubuntu/myproject/build'
/usr/bin/cmake -E cmake_progress_start /home/ubuntu/myproject/build/CMakeFiles 0

Leave a Comment

Cancel reply

Your email address will not be published.