Add GCC options to top of C source file
Asked Answered
P

2

14

Is is possible to put something at the top of the C source file, like

// GCC_OPTIONS=-g,-Wall

that will add those options automatically to gcc every time you compile this file?

Plymouth answered 17/9, 2010 at 0:25 Comment(0)
C
11

Yes it is, at least for some flags. You can push and pop diagnostic settings like this:

#pragma GCC diagnostic error "-pedantic"
#pragma GCC diagnostic warning "-Wall"

This is also possible for optimization levels on a per-function level:

#pragma GCC optimize ("string"...)

These #pragma definitions affect all code after them in a source file.

See these other questions for more information:

Crossfertilization answered 17/9, 2010 at 4:28 Comment(4)
Neither of these are equivalent to the options he's asking about.Strapless
@MatthewFlaschen These settings affect all functions defined later in a source file, so to some extent they can be equivalent: put them on the top and they kind of become global.Corrugate
No debug unfortunately it seems: #2007321Lempres
Is there a pragma for setting an -f/-fno flag (e.g., ` -fno-ipa-icf`)?Struthious
S
1

No. Some compiler options have #define equivalents, but not those. This belongs in your make file (or equivalent). With GNU Make:

CFLAGS += -g -Wall
Strapless answered 17/9, 2010 at 0:28 Comment(2)
-Wall has an in source equivalent.Crossfertilization
@Matt, what is it? Your answer doesn't give one, and #pragma GCC diagnostic warning "-Wall" definitely doesn't work in GCC 4.4.Strapless

© 2022 - 2024 — McMap. All rights reserved.