Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why isn't gcc passing macro value from the command line?

Tags:

gcc

I'm trying to pass the value of a C macro to the preprocessor with the -Dmacro=value option of gcc. However, it doesn't do what i expect. This is the basics of my code:

#define T0 0
#define T1 0
#define T2 0
#define T3 0

int main(){

    int f[9];

    start(f[T0], f[T1], f[T2], f[T3]);

    return 0;
}

Running gcc -DT3=1 -E shows the preprocessor does not replace the original value of T3. What am i missing, and how can the value be passed properly?

like image 498
quetric Avatar asked Mar 30 '12 18:03

quetric


People also ask

How do I pass a macro in makefile?

I usually pass macro definitions from "make command line" to a "makefile" using the option : -Dname=value. The definition is accessible inside the makefile. I also pass macro definitions from the "makefile" to the "source code" using the similar compiler option : -Dname=value (supported in many compilers).

What gcc option will generate the preprocessed source?

The -E option causes gcc to run the preprocessor, display the expanded output, and then exit without compiling the resulting source code. The value of the macro TEST is substituted directly into the output, producing the sequence of characters const char str[] = "Hello, World!" ; .

Which gcc option Undefine a preprocessor macro?

4. Which gcc option undefines a preprocessor macro? Explanation: None.

Can we change macro value in C?

You can't. Macros are expanded by the Preprocessor, which happens even before the code is compiled. It is a purely textual replacement.


1 Answers

Afaik that switch will define a macro, but your code then overrides it back to 0. Remove that define from your code or surround it with ifdef and it should be fine.

like image 185
dbrank0 Avatar answered Oct 11 '22 23:10

dbrank0