74

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186

5 Answers5

156

Add this line to the file CMakeLists.txt:

set(CMAKE_BUILD_TYPE Debug)
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Jespa
  • 1,609
  • 1
  • 10
  • 3
  • 8
    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
126

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 ...
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
guilloptero
  • 1,583
  • 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,551
  • 11
  • 22
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.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Michał Leon
  • 2,108
  • 1
  • 15
  • 15
-10

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
  • 223,805
  • 18
  • 296
  • 547
  • 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
  • 5
    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
  • 1
    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