问题
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)
回答1:
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
来源:https://stackoverflow.com/questions/23560214/how-to-set-flags-of-g-using-cmake-such-that-gprof-can-demangle