[CMake] How can I add gprof profiler to my CMakeLists.txt?

Marcel Loose loose at astron.nl
Tue Dec 15 03:37:01 EST 2009


On Tue, 2009-12-15 at 12:08 +0800, Water Lin wrote:
> I want to use gprof to analyze my code. So I try to embed the -pg
> paramter for g++ in my CMakeLists.txt file.
> 
> But after I embeded the -pg to my CMakeLists.txt like this:
> ---------
> add_definitions(-pg -march=pentium4 -msse3 -g)
> ---------
> and I recompile my code. There is no gprof output file.
> 
> But when I use command like:
> --------
> $ g++ -pg ....
> --------
> to compile my code, gprof will generate a output file gmon.out.
> 
> So, how can I enable gprof profiler in my CMakeLists.txt?
> 
> Thanks
> 
> Water Lin

Hi Water Lin,

>From the g++ info files:

`-pg'
     Generate extra code to write profile information suitable for the
     analysis program `gprof'.  You must use this option when compiling
     the source files you want data about, and you must also use it when
     linking.

So, you should use `-pg' both when compiling and linking. The command
add_definitions() will only add this option during compilation. You
should also add `-pg' to CMAKE_EXE_LINKER_FLAGS or to
CMAKE_EXE_LINKER_FLAGS_[CMAKE_BUILD_TYPE], where CMAKE_BUILD_TYPE could
be, e.g., `DEBUG'.

HTH,
Marcel Loose.




More information about the CMake mailing list