gcc conditional compilation
Asked Answered
L

3

8

I'm learning about conditional compilation and I think that I understand it well so far. Now, if I have the code:

 #ifdef UMP_TO_FILE
    //do something here...
 #endif

and I run:

 gcc myprogram.c -DUMP_TO_FILE

Then, the code block "//do something here..." gets compiled. Now, my question is:

What exactly the -DUMP_TO_FILE flag does?

I think that the flag is "-D" and it defines the macro "UMP_TO_FILE", but I want to be sure of the syntaxis and "gcc --help" does not tell me anything about this and maybe I don't know how to search for this on the Internet!!

Thank you very much for sharing your knowledge!

Leialeibman answered 14/6, 2011 at 1:42 Comment(2)
I've give you a quick answer.. anyway @Andrew White is right. Please start to accept some answers before asking again..Intercalary
Lucy, on the off chance that you don't know what Andrew and 0verbose are talking about, answers that best meet your requirements can be accepted by ticking on the empty green mark to the left of them. It's a good idea to go back over your old questions and do this if any answers are good enough. It will convince some people to more readily answer future questions - not me of course, I'm only in this for the opportunity to foist my opinions on others and I'd probably do it even if it cost me money :-)Fibrinolysin
F
9

The output of man gcc contains this little snippet:

-D name

Predefine name as a macro, with definition 1.

-D name=definition

The contents of definition are tokenized and processed as if they appeared during translation phase three in a #define directive. In particular, the definition will be truncated by embedded newline characters.

If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell's quoting syntax to protect characters such as spaces that have a meaning in the shell syntax.

If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you will need to quote the option. With sh and csh, -D'name(args...)=definition' works.

-D and -U options are processed in the order they are given on the command line. All -imacros file and -include file options are processed after all -D and -U options.

So, the option -DUMP_TO_FILE is equivalent to putting the line #define UMP_TO_FILE 1 in your source code (there are subtle differences but there's no need to cover them here).


As an aside, I consider this rather bad practice. The author is performing trickery so that it looks like a compiler option DUMP_TO_FILE rather than a pre-processor macro. That's sort of clever but it's going to cause trouble for anyone looking at the source wondering what UMP_TO_FILE actually means.

Personally, I'd rather see -DDUMP_TO_FILE and #ifdef DUMP_TO_FILE since it's much clearer in intent.

Fibrinolysin answered 14/6, 2011 at 1:54 Comment(0)
I
4
-D UMP_TO_FILE

Defines the macro value UMP_TO_FILE. It's like to put in your code the following line:

#define UMP_TO_FILE

If the value (UMP_TO_FILE) is defined, the the lines of code between #ifdef and #endif will be compiled. Otherwise not.

Intercalary answered 14/6, 2011 at 1:49 Comment(0)
P
2

-d defines a symbol. I believe most compilers will also allow to assign a value to symbol, as in -dfoo=bar assigns bar to foo symbol.

From http://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#Preprocessor-Options

-D name Predefine name as a macro, with definition 1.

-D name=definition The contents of definition are tokenized and processed as if they appeared during translation phase three in a `#define' directive. In particular, the definition will be truncated by embedded newline characters. If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell's quoting syntax to protect characters such as spaces that have a meaning in the shell syntax.

If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you will need to quote the option. With sh and csh, -D'name(args...)=definition' works.

-D and -U options are processed in the order they are given on the command line. All -imacros file and -include file options are processed after all -D and -U options.

Palestine answered 14/6, 2011 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.