How to disable compiler optimizations when building through SFDK?

When building a project using sfdk build or through the build button in Qt Creator, something sets CXXFLAGS='-O2 -g …' and CFLAGS to an equivalent value. Is it possible to easily force the compiler to disable the compiler optimisations by removing the -O2 flag and setting the -O0 flag, as it sometimes optimises code out, making it harder to debug with gdb.

Isn’t it enough to just add -O0? No need to remove -O2 afaik.

Ah, it seems to work by editing the makefiles/cmake files and making sure to -O0 flag is after the -02.

Hello!

I’m using the emulator targeting SFOS 5.1.0.11-1-2026.06.30-1 and I’ve had the same issue, in a project using qmake with a .pro file that doesn’t touch optimization flags, I’ve struggled to find how to disable them in debug altogether.

I’d like to share my findings and solution, in the hope of being useful to others or to get a better solution :slight_smile:

It seems that the -O2 in the CXXFLAGS come from this file:
mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/share/qt5/mkspecs/qmodule.pri

Which does:

QMAKE_CFLAGS = -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -m32 -msse -msse2 -march=i686 -mfpmath=sse -mtune=generic -fno-omit-frame-pointer -fasynchronous-unwind-tables
QMAKE_CXXFLAGS = -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -m32 -msse -msse2 -march=i686 -mfpmath=sse -mtune=generic -fno-omit-frame-pointer -fasynchronous-unwind-tables

So in my .pro file, I’ve added this to effectively disable optimizations:

CONFIG(debug) {
    # replace the previous optim value set by the SDK in /usr/share/qt5/mkspecs/qmodule.pri
    QMAKE_CFLAGS_DEBUG ~= s/-O2/-O0/
    QMAKE_CXXFLAGS_DEBUG ~= s/-O2/-O0/
}

But this caused a warning (treated as error in my project), because of _FORTIFY_SOURCE=2 define being incompatible with optimizations turned off:

In file included from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/opt/cross/include/c++/13.4.0/i486-meego-linux-gnu/bits/os_defines.h:39,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/opt/cross/include/c++/13.4.0/i486-meego-linux-gnu/bits/c++config.h:679,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/opt/cross/include/c++/13.4.0/cstddef:49,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/include/qt5/QtCore/qglobal.h:39,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/include/qt5/QtCore/qchar.h:37,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/include/qt5/QtCore/qstring.h:41,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/include/qt5/QtCore/qdatetime.h:37,
                 from /home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/include/qt5/QtCore/QDate:1,
                 from ../harbour-lerouter/src/pch.h:8:
/home/belkiss/RD/sailfish/SailfishOS/mersdk/targets/SailfishOS-5.1.0.11EA-i486.default/usr/include/features.h:435:4: error: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Werror=cpp]
  435 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
      |    ^~~~~~~

that I solved by editing the .pro file again to define it to 0 instead of 2:

CONFIG(debug) {
    # replace the previous optim value set by the SDK in /usr/share/qt5/mkspecs/qmodule.pri
    QMAKE_CFLAGS_DEBUG ~= s/-O2/-O0/
    QMAKE_CXXFLAGS_DEBUG ~= s/-O2/-O0/
    # disable _FORTIFY_SOURCE as it expects compiler optimizations to be on
    QMAKE_CFLAGS_DEBUG ~= s/_FORTIFY_SOURCE=2/_FORTIFY_SOURCE=0/
    QMAKE_CXXFLAGS_DEBUG ~= s/_FORTIFY_SOURCE=2/_FORTIFY_SOURCE=0/
}

With this the warning is gone, the build succeeds and optims are effectively disabled in Debug :slight_smile:

I wonder if the qmodule.pri could be changed so that the flags could only be added in release, maybe using QMAKE_CFLAGS_RELEASE / QMAKE_CXXFLAGS_RELEASE?

Cheers