Groups > Borland > Borland C plus plus builder command line tools > Re: Need help with #if defined() used with SET environment VARIABLE at command line.




Need help with #if defined() used with SET environment
VARIABLE at command line.

Need help with #if defined() used with SET environment VARIABLE at command line.
17 Mar 2007 11:56:20 -0700
I'm using bcc32 5.5.1 on WinXP. Can someone help with doing a #if
defined(MY_SET_VAR) test at compile time. MY_SET_VAR is set with SET
MY_SET_VAR=5 from the WinXP command line.  I am trying the simple
program:

First, from the command line, I do:

set MY_SET_VAR=5

Then compile:

int main() {

   #if defined(MY_SET_VAR)
       printf ("MY_SET_VAR"=%i\n", MY_SET_VAR );
   #else
      printf ("MY_SET_VAR does not exist.\n");
   #endif


   return 0;
}

Then run and program always prints that MY_SET_VAR does not exist.

I'm working on some programs that suggest this is possible in some
way, but I don't know.

Thanks.
Post Reply
Re: Need help with #if defined() used with SET environment VARIABLE at command line.
Sat, 17 Mar 2007 15:17:40 -040
> MY_SET_VAR is set with
> SET MY_SET_VAR=5 from the WinXP command line

Then MY_SET_VAR is an item in the operating system environment and available 
for use on command lines given to the operating system.  However C and C++ 
macros are defined in the program and not in the operating system 
environment.

To have it defined in your program put this in the source code prior to 
where the macro is used.

#define MY_SET_VAR 5

If writing in C++ doing this instead is an alternative and is in some ways.

const int MY_SET_VAR = 5;

.  Ed

> irvpaton wrote in message
> news:1174157780.807471.246210@l77g2000hsb.googlegroups.com...
>
> I'm using bcc32 5.5.1 on WinXP. Can someone help with doing a #if
> defined(MY_SET_VAR) test at compile time. MY_SET_VAR is set with SET
> MY_SET_VAR=5 from the WinXP command line.  I am trying the simple
> program:
>
> First, from the command line, I do:
>
> set MY_SET_VAR=5
>
> Then compile:
>
> int main() {
>
>   #if defined(MY_SET_VAR)
>       printf ("MY_SET_VAR"=%i\n", MY_SET_VAR );
>   #else
>      printf ("MY_SET_VAR does not exist.\n");
>   #endif
>
>   return 0;
> }
>
> Then run and program always prints that MY_SET_VAR does not exist.
>
> I'm working on some programs that suggest this is possible in some
> way, but I don't know.

Post Reply
Re: Need help with #if defined() used with SET environment VARIABLE
Sat, 17 Mar 2007 21:16:57 +010
irvpaton@gmail.com wrote:

> I'm working on some programs that suggest this is possible in some
> way, but I don't know.

Besides what Ed told you, you can try

set MY_SET_VAR=5

and then run this:

#include <iostream>
#include <stdlib.h>
#include <conio.h>

int main(void)
{
   if (getenv("MY_SET_VAR")==NULL)
       std::cout<<"MY_SET_VAR does not
exist.\n"<<std::endl;
   else
     
std::cout<<"MY_SET_VAR="<<getenv("MY_SET_VAR")&l
t;<std::endl;

   getch();
   return 0;
}

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
Post Reply
Re: Need help with #if defined() used with SET environment VARIABLE at command line.
18 Mar 2007 10:04:56 -0700
Thanks both for the good replies. However, I was working with a
prewritten system and it had the #if defined(MY_SET_VAR) to control
things during compile. Someone told me to do this type of thing:

bcc32 -DMY_SET_VAR=5 mycfile.c

which did work.

Irv
Post Reply
about | contact