1

I've been working on adding functionality to a C++ library. The library is compiled by using CMake. It has a complex set of dependencies. I have a C++ test file that runs code relating to the library. Let the compiled file be test.cpp, its executable test.

So far, I've been debugging by adding "cout" statements to the library files. I frequently get segmentation faults, but can usually figure it out by inspection. Obviously, this is inefficient. I want to see where the code fails, by using gdb. Via this stackoverflow post, I tried adding debug flags to my cmake, but when I run gdb on test and do bt, I don't get comprehensive info. I simply get the name of the function in the library where the code fails, not the exact line.

Anyone know how to get the gdb information?

Community
  • 1
  • 1
pyrrhic
  • 1,769
  • 2
  • 15
  • 27
  • The best way to use gdb instead from command line is to use a decent IDE (e.g. Eclipse CDT), to support you with debugging. You'll step through your source code visually synchronized, and be provided with easy shortcuts to set breakpoints etc. There are various IDEs available, that support gdb based debugging. – πάντα ῥεῖ Aug 07 '14 at 18:29
  • I don't want to use an IDE. The code-base I am working on was built to work with CMake, a tool to build C++ libraries via command line. – pyrrhic Aug 07 '14 at 18:31
  • 1
    I well know what CMake does, and as far I can read from your question, the problem you're asking for, isn't really related to CMake, but how to reaonably use gdb! I recommended to use the IDE because it makes it easier to deal with gdb debugging, think twice. – πάντα ῥεῖ Aug 07 '14 at 18:35
  • 2
    We'll need more info to help. You say you've added the `-g` flag to the relevant compile units? What's the sample output from gdb? – Cameron Aug 07 '14 at 18:35
  • @Cameron I only added the flags from the stackoverflow post to the cmake executable, i.e. cmake -DCMAKE_BUILD_TYPE=Debug . I think this should automatically add the '-g' flags to the Makefile. The output from gdb is simply main(), then the function in the library with a seg fault. – pyrrhic Aug 07 '14 at 18:37
  • @πάνταῥεῖ I think it may be related to both, since maybe the debugging info isn't properly getting generated. – pyrrhic Aug 07 '14 at 18:38
  • possible duplicate of [How do you set GDB debug flag with cmake?](http://stackoverflow.com/questions/10005982/how-do-you-set-gdb-debug-flag-with-cmake) – ComicSansMS Aug 08 '14 at 08:48

2 Answers2

1

While adding the respective compiler flags manually will work, it is not the most convenient way of doing so. As suggested by @ruslo, you should use the following command line instead for getting debug support:

cmake -DCMAKE_BUILD_TYPE=Debug <path_to_source>

There are several reasons for this:

  • Compiler flags are not portable. -g -O0 will work on gcc, but what about other compilers? One of CMake's main strengths is to make portability easy, so you should not throw it out of the window easily.
  • Multi-configuration generators (like most IDE generators) allow to use multiple profiles at once. You would not want to force users of those IDEs to compile without optimizations even though they selected a Release profile, would you?
  • Changes to CMAKE_CXX_FLAGS are global. This becomes especially nasty once you have to compose multiple projects together. If you absolutely need to manually give compiler flags, use target_compile_options for this purpose.
  • Last but not least: Setting CMAKE_BUILD_TYPE is the idiomatic solution to this problem. It is the one right tool for solving it and people familiar with CMake (granted, there are not too many around of those...) will be surprised if you solve it using a non-idiomatic workaround.
Community
  • 1
  • 1
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
0

I've figured it out. They key is to add the "-g" flag to

SET (CMAKE_C_FLAGS ...

and

SET(CMAKE_CXX_FLAGS ...

to the CMakeLists.txt file.

pyrrhic
  • 1,769
  • 2
  • 15
  • 27
  • 1
    You may want to add `-O0` to disable optimisation too - this helps the ability to inspect and debug the actual code (e.g you can set a breakpoint on line 305 in somefile.cpp, and then step one line at a time in gdb). This is particular efficient if you already have a reasonable idea of where and why it's failing. If you use optimised code, the compiler will often modify the code to such an extent that it't quite hard to determine where exactly in the original code the problem was). – Mats Petersson Aug 07 '14 at 19:03
  • 2
    As you mentioned above the right way to do it is to use `CMAKE_BUILD_TYPE=Debug` so both `-O0` and `-g` must be added. If it's not work try to remove build directory (directory with CMakeCache.txt). –  Aug 08 '14 at 06:47