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?

7
  • 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. Commented Aug 7, 2014 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. Commented Aug 7, 2014 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. Commented Aug 7, 2014 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? Commented Aug 7, 2014 at 18:35
  • @Cameron I only added the flags from the stackoverflow post to the cmake executable, i.e. cmake -DCMAKE_BUILD_TYPE=Debug <path>. 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. Commented Aug 7, 2014 at 18:37

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

this one is better than the accepted answer.
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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.