How to set flags of g++ using cmake such that gprof can demangle?
Asked Answered
I

1

14

How do I set the gprof flags for the compiler and linker of GNU g++ in a CMakeLists.txt?

My current approach,

set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -pg")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -pg")
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} -pg")

does not allow gprof to demangle the C++ functions. Any ideas? (I am using C++11)

Invasion answered 9/5, 2014 at 8:43 Comment(2)
All that is needed to demangle a function is the function name itself, you can't add extra g++ flags to help AFAIK.Alexandretta
Very similar question: #26492448 There is an answer which does not involve modifying the CMakeLists.txt.Dicotyledon
T
17

Try using:

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 propertly.

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
Thorson answered 30/10, 2014 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.