cmake Variables and Properties Adding profiling flags to CMake to use gprof


Example

The series of events here is supposed to work as follows:

  1. Compile code with -pg option
  2. Link code with -pg option
  3. Run program
  4. Program generates gmon.out file
  5. Run gprof program

To add profiling flags, you must add to your CMakeLists.txt:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")

That must add flags to compile and link, and use after execute the program:

gprof ./my_exe

If you get an error like:

gmon.out: No such file or directory

That means that compilation didn't add profiling info properly.