Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

C/C++ • Re: Pico SDK compilation question re placement of preprocessor directives

$
0
0
You can invoke the preprocessor on its own using "-E" flag. There's not much more to it than a (conditional) text substitution tool. This little example might help..

Code:

foo@sdu:/wrk/T$ cat x.c#include <stdio.h>intmain(void){ printf("Hello World");#ifndef SUPPRESS_LINEFEED putchar('\n');#endif return 0;}
We only need to see the last few lines (hence tail -15) . Note the preprocessor lines have been replaced with whitespace. This is effectively the code the compiler actually attempts to compile..

Code:

foo@sdu:/wrk/T$ gcc -E x.c | tail -15# 902 "/usr/include/stdio.h" 3 4# 2 "x.c" 2# 3 "x.c"intmain(void){ printf("Hello World"); putchar('\n'); return 0;}
If we ensure "SUPPRESS_LINEFEED" is defined then the preprocessor changes the code the compiler will see..

Code:

foo@sdu:/wrk/T$ gcc -DSUPPRESS_LINEFEED -E x.c | tail -15# 902 "/usr/include/stdio.h" 3 4# 2 "x.c" 2# 3 "x.c"intmain(void){ printf("Hello World"); return 0;}
..the "putchar" has gone.

Hopefully it makes sense now why the compiler complained when you moved your code around. It was, in effect, the same as a cut 'n' paste.

Statistics: Posted by swampdog — Sat Feb 03, 2024 1:38 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles