Author Topic: How to give value in Global Compiler Settings #define in CodeBlocks  (Read 4926 times)

Offline nairaca

  • Single posting newcomer
  • *
  • Posts: 3
so I have this code on codeblocks in which changing value of the preprocessor will have different function, here is the sample.

```
#ifdef LOOP_ == 1
   \\some function
#elif LOOP_ == 2
   \\another function
#endif
```

Now, I do also have this on the header file

```
#define LOOP_ 1
```

changing the value on the header file then rebuilding the project works, but then there is more appropriate way to do this. I have done this before but without the value, it is by typing the preprocessor at the #define under the compiler settings at the Global Compiler Settings.

Right now, I have tried to put "LOOP_ 2" but it gives me error saying "No such file or directory" (I guess the white space is for directory not the value), I also tried "LOOP_2" but it doesnt work (well I guess we know why, it is because it is named LOOP_ only). Also tried with equal sign and still no luck, Any help would be appreciated. Thank you

Offline PB

  • Multiple posting newcomer
  • *
  • Posts: 57
Re: How to give value in Global Compiler Settings #define in CodeBlocks
« Reply #1 on: April 24, 2022, 06:09:43 pm »
Use "=" between the define name and value, e.g.
Code
LOOP_=1
it does work for me with C::B v20.03. Just make sure the project is rebuilt after changing the value, as just changing the define in the build options does not seem enough to trigger C::B to make a fresh rebuild.

BTW, I would use #if and not #ifdef to check the value of a macro, but if you say it works for you when the macro is defined in the code I guess it's OK.
« Last Edit: April 24, 2022, 06:16:46 pm by PB »

Offline nairaca

  • Single posting newcomer
  • *
  • Posts: 3
Re: How to give value in Global Compiler Settings #define in CodeBlocks
« Reply #2 on: April 24, 2022, 06:18:04 pm »
Hi, Ive tried this method and after rebuilding, it gives warning: "LOOP_" redefined. But when I Build and Run, it doesnt change the function. It is still at LOOP_ 1

Offline nairaca

  • Single posting newcomer
  • *
  • Posts: 3
Re: How to give value in Global Compiler Settings #define in CodeBlocks
« Reply #3 on: April 24, 2022, 06:38:45 pm »
BTW, I would use #if and not #ifdef to check the value of a macro, but if you say it works for you when the macro is defined in the code I guess it's OK.

Im sorry for confusion, it is #if on my code. But still the function doesn't change. I put "LOOP_=2" and after rebuilding, it gives warning saying that the LOOP_ is redefined (just a warning not an error so I assume that the change takes effect). But when I run the program it is still on LOOP_ 1.

Offline PB

  • Multiple posting newcomer
  • *
  • Posts: 57
Re: How to give value in Global Compiler Settings #define in CodeBlocks
« Reply #4 on: April 24, 2022, 06:58:42 pm »
Well, to me it seems clear that you have the define both in the code and project build options.

Any recent decent compiler will tell you where exactly in the code the redefinition occurs.

You need to decide where to have the define, if you keep it in both places (which I believe to be a bad idea), I think the define in the code will have precedence.

Offline energetictrust

  • Single posting newcomer
  • *
  • Posts: 1
    • geometry dash lite
Re: How to give value in Global Compiler Settings #define in CodeBlocks
« Reply #5 on: August 24, 2023, 05:22:10 am »
To implement a basic #define, select settings > Compiler and type the define into the "Additional compiler options" text box. All projects that use the same compiler settings will share the same define values. Per-project variations on globally-set definitions are possible.