sincerely Singaporean

If you have not done so, read this full tutorial on how to use SGEXTN to build an application.

Debugging Tutorial

Regardless if you just wrote your "Hello, Singapore" (or "hello world") programme today or if you just represented Singapore in the International Olympiad for Informatics, everyone unintentionally writes bugs into our code. The process of catching and fixing these bugs is known as debugging.

Bugs can be incorrect behaviour (function returning 2 when it is supposed to return 1) or crash. To debug incorrect behaviour, you can print out the values of variables to see what went wrong. To debug crashes, you can use a debugger.

SGEXTN has a builtin way of debugging incorrect behaviour using the SG macro. To use it, you must link to the SGEXTN Core module. See BuildLah ⁽㈳㈴㈳㈮㈱㈨㈠㈫ ㈧㈤㈱㈤⁾ for more information about how to link it properly.

In the specific C++ file that you are working on, include the header for SGXDebug which defines the macro.

#include <SGXDebug.h>

Now you can use the SG macro.

Note that whenever the SG macro prints out anything, it will be shown in the Application Output section in your Qt Creator. By default, this is on the bottom bar, the same place where you can access the Qt Creator terminal and perform search operations. Also, if something is being printed, it shows automatically.

Directly writing

SG;

in your code is very helpful for seeing if something run at all. Maybe you forgot to call the function or you had a typo in a conditional's condition, so your code is not being run at all.

When the code execution reaches the point of the SG macro, it will show the file name and line number of the SG macro. By looking at these logs, you can know what actually run.

You can also get the SG macro to print the value of a specific variable. It prints all the SGEXTN builtin structs, most C++ builtin data types, and pointers to void. Note that when printing pointers, you may have to cast them to void* before printing.

To print something, simply put it in brackets behind the SG macro. You can put multiple sets of brackets and the SG macro will print all the stuff sequentially.

Sample debugging code:

int i = 26; float f = 3.14f; SGXString s = "hi"; SG; SG(i); SG(s); SG(i)(f)(s);

Expected output format:

SG - somefile.cpp, line 70 SG - 26 SG - hi SG - 26 - 3.14 - hi

The SG macro is essentially equivalent to std::cout or qDebug, but it is builtin to SGEXTN and can print SGEXTN custom structs. Also, chaining brackets look nicer than chaining ‹‹ operators.

To debug crashes, click the run with debug button in Qt Creator below the run button that looks like the run button with a bug attached to it. This will run the app with debugger enabled, but it may take really long to load.

Once you have found the point where the crash occurs, you can use the SG macro to find which variable is not having a correct value.

©2025 05524F.sg (Singapore)

contact 05524F / report a bug / make a suggestion

about 05524F SINGAPORE values

list of 05524F projects