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 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).
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.
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
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.
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?
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 ?