Search code examples
c++qtqt5qmakesanitizer

How to enable sanitizers in QMake?


How do I enable usage of sanitizers in QMake's .pro files?

I found several ressources that modify QMAKE_CXXFLAGS themselves but the introductory blogpost says:

It is scheduled for the dev branch (Qt 5.2) because it’s a new feature, but you should be fine cherry-picking it to e.g. Qt 5.0. You can then configure Qt with -address-sanitizer, and run qmake CONFIG+=address_sanitizer for your own applications.

However, adding CONFIG+=address_sanitizer does not seem to have an effect.


Solution

  • To enable, e.g., address sanitizer you have to write:

    CONFIG += sanitizer sanitize_address
    

    The sanitizer options are defined in Qt's mkspecs (mkspecs/features/sanitizer.prf and mkspecs/common/sanitize.conf), which gets loaded when you add CONFIG += sanitizer. A quick look into my sanitizer.prf (Qt 5.7.0) shows that the specific sanitizers are:

    • sanitize_address
    • sanitize_memory
    • sanitize_thread
    • sanitize_undefined