Subsection 7.1.2 Part 2a: What the Compiler Is Trying to Tell You
Compiler error messages are the cheapest debugging tool we have: the compiler has already found the problem, but the line it reports is not always the line the mistake is on. Here is a classmate’s
blinky.c that will not build. It is Blinky, and exactly one thing is wrong with it:
#include "stm32c0xx.h" // All register definitions are here
#define GPIOAEN (1U << 0) // RCC->IOPENR bit 0: clock for GPIOA
#define LED_PIN (1U << 5) // GPIOA bit 5: the on-board LED
int main(void) {
RCC->IOPENR |= GPIOAEN // Step 1: enable GPIOA clock
GPIOA->MODER |= (1U << 10); // Step 2a: set bit 10
GPIOA->MODER &= ~(1U << 11); // Step 2b: clear bit 11 -> PA5 output
while (1) {
GPIOA->ODR |= LED_PIN; // Step 3: bit 5 = 1 -> LED on
for (int i = 0; i < 100000; i++);
GPIOA->ODR &= ~LED_PIN; // Step 4: bit 5 = 0 -> LED off
for (int i = 0; i < 100000; i++);
}
return 0; // never reached
}
The compiler reports the following. A quick anatomy note for a first read of GCC output: each block starts with
file:line:column:, then
error: (what stopped the compiler) or
note: (context it is adding, pointing at a second, related location), then the offending source line with a caret marking the exact column.
blinky.c: In function 'main':
blinky.c:2:18: error: called object is not a function or function pointer
2 | #define GPIOAEN (1U << 0) // RCC->IOPENR bit 0: clock for GPIOA
| ^
blinky.c:6:21: note: in expansion of macro 'GPIOAEN'
6 | RCC->IOPENR |= GPIOAEN // Step 1: enable GPIOA clock
| ^~~~~~~
Activity 7.1.3. Find the Real Bug.
(a)
The first message points at line 2. Read line 2 of the listing. Is anything wrong with it?
(b)
Decide which line the actual bug is on, and what the fix would be. Commit to an answer before reading on.
Line 2 is perfectly healthy. The real bug is the missing semicolon at the end of line 6. Both
GPIOAEN and
GPIOA are macros that expand to parenthesized expressions. With the semicolon gone, the compiler sees the value
(1U << 0) followed immediately by a parenthesized expression,
GPIOA’s own expansion, and in C, a value followed by a parenthesized list is a
function call. So it objects to the thing being “called”: the value of
GPIOAEN, defined on line 2. (Had
GPIOA been an ordinary variable, the same missing semicolon would have produced a plain “expected ’;’”. The message is baffling only because the peripheral name is itself a parenthesized macro.) The line where an error is
reported is often just where the compiler finally gave up making sense of the code.
Which direction should you look? Usually the mistake sits at or
above the reported line, but this example is exactly the case where that fails, and it is worth seeing why. Line 2 is a
#define, and a
#define is not code that runs where it is written; it is text pasted in wherever the macro is
used. The compiler reported the error at the definition, then told you where the paste happened:
note: in expansion of macro 'GPIOAEN', line 6. Line 6 is the line it was really reading, and the missing semicolon is at the end of it,
below line 2, not above.
So the rule is to read up from the line the compiler was actually reading. For ordinary code that is the reported line; when the message names a macro, it is the line given in the
note:, which is wherever the macro was used. Three rules recover almost every case:
-
Start at the top. One root error can spawn many messages; the first one is closest to the cause, and fixing it often clears several below it.
-
Read up, from the right line. If the reported line looks healthy, the bug is usually on an earlier line: a missing semicolon, ), or } above. But when the message names a macro, the reported line is the #define; read up from the line in the note: in expansion of macro instead, which is wherever the macro was used.
-
Isolate when stuck. For a really pesky error, comment out a block of code and rebuild. If the error moves, it tells you a bug was inside the commented out code.
It is worth doing this once in your own project after today: delete that semicolon, build the project, read the message the compiler gives you, and then put the semicolon back. Seeing the message your own compiler produces for a fault you created on purpose is what makes it recognizable the next time it appears by accident.
A classmate’s Blinky that will not build. Exactly
one thing is wrong with it:
#include "stm32c0xx.h" // All register definitions are here
#define GPIOAEN (1U << 0) // RCC->IOPENR bit 0: clock for GPIOA
#define LED_PIN (1U << 5) // GPIOA bit 5: the on-board LED
int main(void) {
RCC->IOPENR |= GPIOAEN // Step 1: enable GPIOA clock
GPIOA->MODER |= (1U << 10); // Step 2a: set bit 10
GPIOA->MODER &= ~(1U << 11); // Step 2b: clear bit 11 -> PA5 output
while (1) {
GPIOA->ODR |= LED_PIN; // Step 3: bit 5 = 1 -> LED on
for (int i = 0; i < 100000; i++);
GPIOA->ODR &= ~LED_PIN; // Step 4: bit 5 = 0 -> LED off
for (int i = 0; i < 100000; i++);
}
return 0; // never reached
}
The compiler’s report. (1) Read line 2: is anything wrong with it? (2)
Decide which line the actual bug is on, and what the fix is:
blinky.c: In function 'main':
blinky.c:2:18: error: called object is not a function or function pointer
2 | #define GPIOAEN (1U << 0) // RCC->IOPENR bit 0: clock for GPIOA
| ^
blinky.c:6:21: note: in expansion of macro 'GPIOAEN'
6 | RCC->IOPENR |= GPIOAEN // Step 1: enable GPIOA clock
| ^~~~~~~
-
Line 2 is perfectly healthy. The bug: the missing semicolon at the end of line 6.
-
With it gone, the compiler sees GPIOAEN’s value followed by a parenthesized expression (GPIOA is a macro too), and in C, a value followed by ( ... ) reads as a function call. So the error points at the “called” value: the macro defined on line 2.
-
The reported line is where the compiler gave up. Usually the mistake is at or above it, but not here.
-
A #define is pasted in wherever the macro is used, so the compiler reported the definition (line 2) and then said where the paste happened: note: in expansion of macro, line 6. Line 6 is the line it was really reading, and the bug is below line 2, not above it.
Three rules recover almost every compiler error:
-
Start at the top: one root error can spawn many messages; fixing the first often clears the rest.
-
Read up, from the right line: when the reported line looks healthy, the mistake is usually at or above it. But if the message names a macro, the reported line is the #define; read up from the note: in expansion of macro line instead, wherever the macro was used. (That is today’s example: #define on line 2, bug on line 6.)
-
Isolate when stuck: comment out a block and rebuild. If the error moves, a bug was inside the commented-out code.