How to use gprof with cmake
Asked Answered
S

2

35

I have looked at dozens of tutorials for profiling with gprof. I'm trying to work with code for the SMT solver dReal. To build the program, I first installed g++-4.8, Bison, Flex, and Cmake. Then to build dReal, the instructions said to execute the following:

git clone [email protected]:soonhokong/dReal.git dreal  
cd dreal  
mkdir -p build/release  
cd build/release  
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_CXX_COMPILER=g++-4.8 -DCMAKE_C_COMPILER=gcc-4.8 ../../src  
make

Where in here am I supposed to add the -pg? I'm new to programming, so reasoning as opposed to an answer would be much appreciated. I know you're supposed to add the -pg to the compilation stage but all the tutorials say to put it after g++ or gcc. I can't find how to modify the code for cmake. I did try changing the build type to PROFILE but that didn't help.

Stripling answered 21/10, 2014 at 16:45 Comment(2)
FYI: https://mcmap.net/q/15228/-capturing-function-exit-time-with-__gnu_mcount_ncGreatniece
Your question is answered hereMyrica
T
33

In order to use gprof, you need to both compile and link with the -pg option.

CMakeLists.txt should have a setup like Ami Tavory mentioned, where command line options are combined with hardcoded options, so a good way to do this with a well built cmake project is:

cmake -DCMAKE_CXX_FLAGS=-pg -DCMAKE_EXE_LINKER_FLAGS=-pg -DCMAKE_SHARED_LINKER_FLAGS=-pg <SOURCE_DIR>
Thoma answered 1/12, 2016 at 23:5 Comment(1)
I'm developing a pathological dislike for CMAKE. 45 mins and nothing I've done actually changes the build. cmake seems to completely ignore the provided args. 2nd time through after wasting 30 mins profiling something (spoiler alert - it didn't profile), I did a clean build and looked at the output and -pg was never invoked. cmake seems to blissfully ignore this kind of instruction. Give me a boring Makefile any day :( [Note: trying this on a different project, so maybe it's this project not cmake]Loritalorn
E
12

I'm Soonho Kong, one of the maintainers of dReal. It would be easier for us to answer questions regarding dReal, if you put the "dreal" tag on the question.

  1. In general, you can pass compiler flags such as -pg to cmake by running:

    cmake -DCMAKE_CXX_FLAGS=-pg

  2. For the profiling, I normally use valgrind + callgrind.

  3. To measure the code coverage, we are using gcov and lcov tools. You may want to take a look at how we set up the compiler flags for that, which can help you set up your own compiler flags for gprof.

Einsteinium answered 16/12, 2014 at 20:30 Comment(4)
This works really well by me too. For future reference, in CMakeLists.txt, I have set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} blah blah blah"), which combines hardcoded stuff with the stuff provided from the cmd line.Jillene
Does passing it like this overwrite other flags set in CMakeLists.txt (or included .cmake files), or is it prepended / appended to the project's MAKE_CXX_FLAGS ? Will this be passed to the linker (I guess the program needs to be linked with -pg as well)?Sparing
It work for me but I added -DCMAKE_C_FLAGS=-pg -DCMAKE_CXX_FLAGS=-pg -DCMAKE_BUILD_TYPE=Debug Then I run application alone after than i use gprof to log profiling. > cmake -DCMAKE_C_FLAGS=-pg -DCMAKE_CXX_FLAGS=-pg -DCMAKE_BUILD_TYPE=Debug .. > make > ./application > gprof ./application gmon.out > profile.txt > gprof ./application -l gmon.out > profile_line.txtShondrashone
gprof and valgrind+callgrind do different things and give you different answers.Tjirebon

© 2022 - 2024 — McMap. All rights reserved.