66

Is there any way to create only symbol table using cmake for gdb ?

Vadim Kotov
  • 7,766
  • 8
  • 46
  • 61
Vivek Goel
  • 21,134
  • 28
  • 101
  • 178

5 Answers5

149

Add this line to the file CMakeLists.txt:

set(CMAKE_BUILD_TYPE Debug)
Aaron Hill
  • 3,186
  • 1
  • 17
  • 33
Jespa
  • 1,523
  • 1
  • 9
  • 3
  • 7
    This also disables optimization. For profiling instead of debugging, you generally want both optimization and symbols (aka -g O2). How to do this with clang? – galinette Feb 11 '20 at 17:13
  • I added this flag to my cmake file however I dont see any difference in the size of the binary which is usually increased after adding `-g` which flag for adding debug symbols. – Krishna Oza Nov 04 '20 at 16:27
121

compile in Release mode optimized but adding debug symbols, useful for profiling :

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ...

or compile with NO optimization and adding debug symbols :

cmake -DCMAKE_BUILD_TYPE=Debug ...
guilloptero
  • 1,525
  • 1
  • 10
  • 6
  • Note that RelWithDebInfo is great, but also changes the optimization from -O3 to -O2 due to historic reasons: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/591 – Ax3l Mar 18 '22 at 20:48
0

If you need the debug symbols for profiling then paste this into CMakeLists.txt:

set(CMAKE_BUILD_TYPE RelWithDebInfo)
Hadus
  • 1,344
  • 11
  • 21
0

If you're using QtCreator, remove (or comment out) any line containing CMAKE_BUILD_TYPE:

# set(CMAKE_BUILD_TYPE Debug) 
# set(CMAKE_BUILD_TYPE Release) <- comment out such lines 

Reason: if you explicitly set CMAKE_BUILD_TYPE in your CmakeLists.txt, QtCreator will not allow you to set any other target.

Michał Leon
  • 1,926
  • 1
  • 14
  • 12
-9

The usual way to produce debugging information for gdb is to pass -g to the gcc or g++ compiler (and also at linking time).

Look into the Cmake FAQ for how to get a debuggable executable.

Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509
  • I want only symbol table is it possible ? – Vivek Goel Nov 03 '11 at 06:11
  • 1
    It probably is possible, but it is not a `cmake` specific question. I think that they are some Linux utilities (perhaps binutils related) to extract debugging information from an ELF executable, but I never used them. http://www.moythreads.com/wordpress/2009/08/31/debugging-information-in-separate-files/ – Basile Starynkevitch Nov 03 '11 at 06:16
  • 4
    When folks ask a question it is rare that they wanted to be pointed to a manual. Thanks but no thanks.... – Ramesh Kadambi Oct 27 '17 at 19:46
  • it IS a cmake question. With gcc or clang, when profiling, you generally enable both symbols and optimization (-g -O2). Whereas with clang you have optimization enabled in release mode, and symbols in debug mode. – galinette Feb 11 '20 at 17:12