Important: Please read the Qt Code of Conduct -

How to compile with debug symbol?

  • Hello everyone,

    Could somebody give me some advice?

    I am learning QT. But there is a problem. Segmentation fault often occurs when I execuse the application that I designed. I know there is some error in the code. But I don't know where the error is. So I set some configuration. Then there is a core file created when a segmentation fault happens. Then I debug it by GDB. But it can't load the debug symbols.
    The result is something like this.
    But I hope it can show me the code like this.
    Or if it can't, I hope it can show me the line number at least.

    Someone tell me that I should compile it with -g. But I don't know how?
    And other people tell I to add CONFIG += debug, but it doesn't work.

    So could somebody give me some help?
    Thanks in advance!

  • Lifetime Qt Champion

    Make sure you set the build to be in debug mode.
    alt text

  • @mrjj
    Thanks. It is in debug mode. But it still doesn't work.

    And sometimes I want it to be release mode. So I can't debug in Release mode?

    And how about this function?
    Can it print debug info in release mode?

    I think sometimes we want to know what happened about the application that we released.

  • Lifetime Qt Champion


    In release mode, there is no debug symbols and it will show assembler.

    Using printf is not related to debug / release mode.
    It's simply your own logging.
    however, if you mean backtrace_symbols, you must consult its docs
    to see if that need debug symbols or not.

  • @mrjj
    OK. Thanks.

    So I can't know where the error is when segmentation fault happens?

  • Lifetime Qt Champion

    well you might be able to get a call stack out to see where it was etc.
    So mostly it will have far less info when in release mode.
    depending on the platform, there might also be other tools you can use.

  • Moderators

    @qingshui-kong said in How to compile with debug symbol?:

    So I can't know where the error is when segmentation fault happens?

    if you get a segfault in release but not in debug, than in 95% of all cases, it's due to uninitialized Pointers or variables the other 5 % are race conditions

  • @mrjj
    The application runs on Ubuntu. I use backtrace to get the call stack.

  • @j-hilk

    Yes, I know the most reasons. But I don't know where the error is. If there are lots of codes, I think it's hard to locate the error. I don't think it's a good way to check the code line by line.

    You are right. I should do something to prevent his knid of error. But how about if I didn't do that in advance?

    Yes. It's my duty to make sure there is no error when I design the code. But I think maybe it is impossible to design a software without error. I want to know how could I locate the error when it happens.

  • Moderators


    Well in such cases, I would suggest making liberal use of qDebug()

    void myClass::myFunction(){
          qDebug() << Q_FUNC_INFO;

    you could even reroute the qDebug output to a file if you really need to. The nice thing about this. You can turn the whole qDebug output off with a single line of code, if you want to actually release your application:


    keep in mind, if your have stuff like

    qDebug() << "File opend" <<;

    the file will not be opend, when the debug output is turned off

  • @j-hilk

    OK. Thank you very much!

Log in to reply