-1

In my code I had a macro:

#define TPS 1(or 0)
int main()
{
....
   if(var)
   {
#ifdef TPS
do something
#endif
   }
}

but now, I want to merge the if(var) with the macro so that I acheive:

int var=1;
#define TPS   (if(var))

int main()
{
   int a, b, c;
   a=1;b=2;c=3;
#if TPS
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
#endif
   printf("++a: %d\n", ++a);
   return 0;
 }

i.e. the block of code inside the macro conditionals should be present only if var=1 eg, for var=1:

int main()
{
   int a, b, c;
   a=1;b=2;c=3;
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
   printf("++a: %d\n", ++a);
   return 0;
 }

and, for var=0:

int main()
{
   int a, b, c;
   a=1;b=2;c=3;
   printf("++a: %d\n", ++a);
   return 0;
 }

How can I implement #define TPS to achieve this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • 1
    What exactly do you want to achieve at compilation time and at run time? Your question is not really clear, and makes me think you don't understand well the difference. – Basile Starynkevitch Jul 31 '13 at 07:52
  • Notice that `var` has a value only at runtime. For the compiler, it is just a *name* of some static location containing an `int`. During *compilation*, `var` does not have any value, it has some value only at *runtime*! – Basile Starynkevitch Jul 31 '13 at 08:04
  • @BasileStarynkevitch, I understand that, I updated the question, I hope it makes more sense now. I am want the preprocessor to replace `TPS` with some conditions in the code which can cover the entire `#if #endif` block – brokenfoot Jul 31 '13 at 08:06
  • I believe you don't understand well how the C compiler (and its preprocessing) works. – Basile Starynkevitch Jul 31 '13 at 08:08
  • Why exactly to you ask? And what real code do you have? Please explain much more what you want to achieve, and what should happen at compilation time, and at run time. – Basile Starynkevitch Jul 31 '13 at 08:17

2 Answers2

3

You cannot do what you are dreaming of.

Preprocessing is one of the earliest phase of the compiler (e.g. gcc). And your TPS looks like you want it to have the compilation behavior depends on runtime variable var. Conceptually, the compiler is first preprocessing your source. You can use gcc -C -E to get the preprocessed textual form.

At compilation time, a variable has a name and the compiler will find its location (but a variable does not have any value during compilation). At runtime, a variable has a location containing a value. Values don't exist at compilation time, so you can't use them in the preprocessing phase.

However, the preprocessing can be conditionnal, like

#if WANTPRINT
   printf("a: %d\n", a);
#endif

and then you could pass (or not) the -DWANTPRINT=1 flag to the compiler.

You could code

int var;
int main() {
  int a, b, c;
  a=1;b=2;c=3;
  if (var) {
    printf("a: %d\n", a);
    printf("b: %d\n", b);
    printf("c: %d\n", c);
  };
  printf("++a: %d\n", ++a);
  return 0;
}

BTW, perhaps you want to dynamically load some code at runtime? On Linux and most Posix systems you can call dlopen(3) and dlsym. You could even generate some C code in some (temporary) file, fork a process to compile it to a shared object, and dlopen that shared object, get a function pointer with dlsym then call it... See also this answer.

FWIW, Common Lisp has a very powerful macro system and is able to "compile" at runtime, and to do arbitrary computations at "compile-time". Actually, SBCL may generate good machine code while running....


Addenda

Perhaps you want to customize the behavior of the GCC compiler itself. Then you might consider using MELT (a domain specific language to extend GCC). But GCC don't enable customization of its preprocessing yet (but mostly of its middle-end, working on internal GCC representations like Gimple)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Ok.. I think I should rephrase it, I am looking for something like a conditional something like: `#define TPS do{some condition that checks the value & enables my macro}while(0)` – brokenfoot Jul 31 '13 at 07:47
  • @tarun27sh Are you looking for the `#if`, `#ifdef`, `#else`, `#endif`, etc. preprocessor directives? –  Jul 31 '13 at 07:48
  • Anything, if it can check my variable and then be `TRUE` or `FALSE` – brokenfoot Jul 31 '13 at 07:51
0

Lose the var integer:

#define TPS 1
#if TPS == 1
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
#endif

Just checking if it is defined can be done with #ifdef:

#define TPS
#ifdef TPS
   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
#endif

More here: http://en.wikipedia.org/wiki/C_preprocessor

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • No, I want the value `#define TPS` to be dependent on the variable `var`. – brokenfoot Jul 31 '13 at 07:50
  • 3
    @tarun27sh as said in other answers, that's not possible because of the nature of the preprocessor. The preprocessor runs at **compile** time, you want to do stuff at **runtime**. Use a normal `if` for that. – Bart Friederichs Jul 31 '13 at 07:51