# Complile C++ for Profiling

**URL:** https://discourse.cmake.org/t/complile-c-for-profiling/3055
**Category:** Usage
**Tags:** os:linux, gen:makefiles
**Created:** [March 31, 2021, 1:05pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055 "2021-03-31T13:05:17Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Master\_Andreas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/master_andreas/32/1339_2.png) [@Master\_Andreas](https://discourse.cmake.org/u/Master_Andreas)
#### Post date: [March 31, 2021, 1:05pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/1 "2021-03-31T13:05:17Z")

</div>

I want to compile my C++ project (using CMAKE)  
I want to use all the perfomance optimaizations of -DCMAKE\_BUILD\_TYPE=Release  
Except striping it of debug linkining (to allow the profiler to record the function calls with names)

I am using GCC 10.2 profilers: perf and callgrind

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 31, 2021, 3:45pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/2 "2021-03-31T15:45:36Z")

</div>

I would use `RelWithDebInfo`. I think it is usually `-O2` rather than `-O3`, but it keeps debugging information. For profiling, you’ll need to manually add other flags like `-fprofile-arcs` and (probably) other compiler-specific flags (for `gprof`). `perf` and `callgrind` generally work with a plain `RelWithDebInfo` build (at least that’s how I use them).

---

<div class="post-metadata">

### Author: ![Master\_Andreas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/master_andreas/32/1339_2.png) [@Master\_Andreas](https://discourse.cmake.org/u/Master_Andreas)
#### Post date: [April 1, 2021, 11:14am UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/3 "2021-04-01T11:14:53Z")

</div>

Callgrind works with:

```auto
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. && make && valgrind --tool=callgrind ./testWarehouse -c ../config.yaml

```

but perf does not

```auto
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. && make && perf record -g ./testWarehouse -c ../config.yaml

```

 ![image](https://discourse.cmake.org/uploads/default/original/2X/7/70f275cb936bd1a3540dd62bff3e75097d7fa497.png)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 2, 2021, 12:03pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/4 "2021-04-02T12:03:36Z")

</div>

That seems like some optimization getting tracked. You could try decreasing the optimization level perhaps? There are probably some `-f` and/or `-g` flags which can keep more debugging information around for `perf`.

---

<div class="post-metadata">

### Author: ![AD2605](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ad2605/32/1856_2.png) [@AD2605](https://discourse.cmake.org/u/AD2605)
#### Post date: [October 6, 2021, 10:22am UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/5 "2021-10-06T10:22:26Z")

</div>

Hello

I do use `RelWithDebInfo` and compile the program with the `-g` flag using `CMAKE_CXX_FLAGS "-g"` however, I am unable to see the function name or the source code in KCachegrind.

How can I set the Cmake flags so as to achieve the same

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 6, 2021, 12:17pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/6 "2021-10-06T12:17:00Z")

</div>

Your target function might be inlined and as such not have any function name actually associated with it. More information is needed to determine what might be the cause here.

---

<div class="post-metadata">

### Author: ![AD2605](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ad2605/32/1856_2.png) [@AD2605](https://discourse.cmake.org/u/AD2605)
#### Post date: [October 6, 2021, 12:30pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/7 "2021-10-06T12:30:30Z")

</div>

Thank you for the quick response !  
Sorry for the lack of information, I had completely missed it.

My Cmake is as follows -

```auto
cmake_minimum_required(VERSION 3.10)
project(mytest)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_CXX_FLAGS_DEBUG "-g")

#set(BLA_VENDOR Intel10_64lp)

#find_package(BLAS REQUIRED)

add_executable(test main.cpp)
#target_link_libraries(test ${BLAS_LIBRARIES})

```

and my `main.cpp` is as follows -

```auto
#define min(x,y) (((x) < (y)) ? (x) : (y))

#include <iostream>

using namespace std;

double min(double x, double y){
	return x < y ? x : y ;
}

int runBlas(){

double a = 34234.39082344;
double b = 12133231.3402;

double c = min(a, b);

return int(c);

}

int main()
{
	runBlas();
        return 0;
}

```

and I call the Valgrind as follows-

```auto
valgrind --tool=callgrind ./test

```

I even try

```auto
valgrind --tool=callgrind --dsymutil=yes ./test

```

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 6, 2021, 4:36pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/8 "2021-10-06T16:36:02Z")

</div>

If this is what you’re actually doing this is what compiler explorer shows with -O2 -g:

```auto
min(double, double):
        minsd xmm0, xmm1
        ret
runBlas():
        mov eax, 34234
        ret
main:
        xor eax, eax
        ret
_GLOBAL__sub_I_min(double, double):
        sub rsp, 8
        mov edi, OFFSET FLAT:_ZStL8__ioinit
        call std::ios_base::Init::Init() [complete object constructor]
        mov edx, OFFSET FLAT:__dso_handle
        mov esi, OFFSET FLAT:_ZStL8__ioinit
        mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
        add rsp, 8
        jmp __cxa_atexit

```

To sum up, nothing is being called and (almost) all your functions reduced to returning a constant. There’s no profile to show for this.

---

<div class="post-metadata">

### Author: ![AD2605](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ad2605/32/1856_2.png) [@AD2605](https://discourse.cmake.org/u/AD2605)
#### Post date: [October 6, 2021, 4:53pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/9 "2021-10-06T16:53:07Z")

</div>

Thank you for your response @fenrir

For a better example code, the -g flag should be enough for it to show me a call graph and everything with my function names (and source code as well) in KCachegrind?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 6, 2021, 4:55pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/10 "2021-10-06T16:55:31Z")

</div>

Yes, `-g` is suitable, though the optimization level may interfere and end up inlining bits of code.

Note that CMake already passes this along in the `RelWithDebInfo` and `Debug` build configurations, so there’s no need to set the flag manually.

---

<div class="post-metadata">

### Author: ![AD2605](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ad2605/32/1856_2.png) [@AD2605](https://discourse.cmake.org/u/AD2605)
#### Post date: [October 6, 2021, 4:59pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/11 "2021-10-06T16:59:43Z")

</div>

Oh okay. Actually I have large codebase for which I would like to generate a callgraph with all the debugging symbols, it would become much easier to familiarize myself with it and I will try it again.

I set build type as `Debug` and and added the `-g` flag but it did not work.  
BTW, I build the project using `catkin_make`, that should not have an effect right ?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 6, 2021, 5:22pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/12 "2021-10-06T17:22:37Z")

</div>

The Debug build type _already_ has `-g` in its flag list, so there’s no need to add it manually.

> `catkin_make`

As long as it can process POSIX Make recipes, sure.

---

<div class="post-metadata">

### Author: ![AD2605](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ad2605/32/1856_2.png) [@AD2605](https://discourse.cmake.org/u/AD2605)
#### Post date: [October 6, 2021, 5:24pm UTC](https://discourse.cmake.org/t/complile-c-for-profiling/3055/13 "2021-10-06T17:24:31Z")

</div>

I suppose it can, I will try it out, Thanks a lot @ben.boeckel for your assistance
