Board index » cppbuilder » 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.


2007-03-18 02:56:20 AM
cppbuilder24
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.
 
 

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

Quote
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
Quote
irvpaton wrote in message
news: XXXX@XXXXX.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.
 

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

XXXX@XXXXX.COM wrote:
Quote
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")<<std::endl;
getch();
return 0;
}
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

{smallsort}

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

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