By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

How can I conditionally compile C++ code based on an environment v

P: n/a
I had two Visual Studio .NET C++ solutions that I combined into one. These
project solutions were very similar. In fact, the two solutions were sharing
many files.
Now that they are one solution, I #ifdef'd pre-processor variables
throughout the code that will compile the code differently. The solution
needs to be #define'd differently depending on which computer I compile it
on:
#define COMPUTER1
//#define COMPUTER2 // comment out above and uncomment this when
compiling on COMPUTER2
....
#ifdef COMPUTER1
.... code specific for COMPUTER1
#endif
#ifdef COMPUTER2
... code specific for COMPUTER2
#endif
My question: Instead of having a "#define COMPUTER1", how can I use an
environment variable (or something similar) that the compiler can use to
determine what computer the program is being compiled on?
Something like:
[assume an environment variable called COMPUTER_NAME which is = "COMPUTER1"
or "COMPUTER2"]
#ifdef GET_ENVIRONMENT_VAR(COMPUTER_NAME)
.... "GET_ENVIRONMENT_VAR()" translates to "COMPUTER1" or "COMPUTER2"
....
?

--
Arthur
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You could do a

#if _MSC_VER

if you are just concerned about the VS.NET version (since each VS version
uses a different compiler version).

--
Regards,
Nish [VC++ MVP]
"Arthur" <ar******@nospam.nospam> wrote in message
news:DE**********************************@microsof t.com...
I had two Visual Studio .NET C++ solutions that I combined into one. These
project solutions were very similar. In fact, the two solutions were
sharing
many files.
Now that they are one solution, I #ifdef'd pre-processor variables
throughout the code that will compile the code differently. The solution
needs to be #define'd differently depending on which computer I compile it
on:
#define COMPUTER1
//#define COMPUTER2 // comment out above and uncomment this when
compiling on COMPUTER2
...
#ifdef COMPUTER1
... code specific for COMPUTER1
#endif
#ifdef COMPUTER2
.. code specific for COMPUTER2
#endif
My question: Instead of having a "#define COMPUTER1", how can I use an
environment variable (or something similar) that the compiler can use to
determine what computer the program is being compiled on?
Something like:
[assume an environment variable called COMPUTER_NAME which is =
"COMPUTER1"
or "COMPUTER2"]
#ifdef GET_ENVIRONMENT_VAR(COMPUTER_NAME)
... "GET_ENVIRONMENT_VAR()" translates to "COMPUTER1" or "COMPUTER2"
...
?

--
Arthur


Nov 17 '05 #2

P: n/a
Thank you for your quick reply.

"#if _MSC_VER" probably won't work since I'm using *identical* compilers on
both computers. In fact, the computers themselves are identical except for
the executables that I want to run on each.

To make an analogy, I have these two computers that each has a program to
inspect a process on a mfg line. Each will inspect a different type of
product. One computer & hardware inspects only capsules, the other only pill
tablets. The program written by me works for both inspection processes, but
there are differences in the program that requires different code depending
on tablets or capsules. Everything else is identical.

How can I compile the 'capsule' code on the 'capsule' computer automatically
w/o changing a pre-processor variable, or something like that?

--
Arthur
"Nishant Sivakumar" wrote:
You could do a

#if _MSC_VER

if you are just concerned about the VS.NET version (since each VS version
uses a different compiler version).

--
Regards,
Nish [VC++ MVP]
"Arthur" <ar******@nospam.nospam> wrote in message
news:DE**********************************@microsof t.com...
I had two Visual Studio .NET C++ solutions that I combined into one. These
project solutions were very similar. In fact, the two solutions were
sharing
many files.
Now that they are one solution, I #ifdef'd pre-processor variables
throughout the code that will compile the code differently. The solution
needs to be #define'd differently depending on which computer I compile it
on:
#define COMPUTER1
//#define COMPUTER2 // comment out above and uncomment this when
compiling on COMPUTER2
...
#ifdef COMPUTER1
... code specific for COMPUTER1
#endif
#ifdef COMPUTER2
.. code specific for COMPUTER2
#endif
My question: Instead of having a "#define COMPUTER1", how can I use an
environment variable (or something similar) that the compiler can use to
determine what computer the program is being compiled on?
Something like:
[assume an environment variable called COMPUTER_NAME which is =
"COMPUTER1"
or "COMPUTER2"]
#ifdef GET_ENVIRONMENT_VAR(COMPUTER_NAME)
... "GET_ENVIRONMENT_VAR()" translates to "COMPUTER1" or "COMPUTER2"
...
?

--
Arthur


Nov 17 '05 #3

P: n/a
Arthur wrote:
<snip>

My question: Instead of having a "#define COMPUTER1", how can I use an
environment variable (or something similar) that the compiler can use
to determine what computer the program is being compiled on?


Use the /D compiler switch.

Arnaud
MVP - VC
Nov 17 '05 #4

P: n/a
You can use the CL environment variable. See
http://msdn2.microsoft.com/en-us/library/kezkeayy.aspx

--
Regards,
Nish [VC++ MVP]
"Arthur" <ar******@nospam.nospam> wrote in message
news:28**********************************@microsof t.com...
Thank you for your quick reply.

"#if _MSC_VER" probably won't work since I'm using *identical* compilers
on
both computers. In fact, the computers themselves are identical except
for
the executables that I want to run on each.

To make an analogy, I have these two computers that each has a program to
inspect a process on a mfg line. Each will inspect a different type of
product. One computer & hardware inspects only capsules, the other only
pill
tablets. The program written by me works for both inspection processes,
but
there are differences in the program that requires different code
depending
on tablets or capsules. Everything else is identical.

How can I compile the 'capsule' code on the 'capsule' computer
automatically
w/o changing a pre-processor variable, or something like that?

--
Arthur
"Nishant Sivakumar" wrote:
You could do a

#if _MSC_VER

if you are just concerned about the VS.NET version (since each VS version
uses a different compiler version).

--
Regards,
Nish [VC++ MVP]
"Arthur" <ar******@nospam.nospam> wrote in message
news:DE**********************************@microsof t.com...
>I had two Visual Studio .NET C++ solutions that I combined into one.
>These
> project solutions were very similar. In fact, the two solutions were
> sharing
> many files.
>
>
> Now that they are one solution, I #ifdef'd pre-processor variables
> throughout the code that will compile the code differently. The
> solution
> needs to be #define'd differently depending on which computer I compile
> it
> on:
>
>
> #define COMPUTER1
> //#define COMPUTER2 // comment out above and uncomment this when
> compiling on COMPUTER2
>
>
> ...
>
>
> #ifdef COMPUTER1
> ... code specific for COMPUTER1
> #endif
>
>
> #ifdef COMPUTER2
> .. code specific for COMPUTER2
> #endif
>
>
> My question: Instead of having a "#define COMPUTER1", how can I use an
> environment variable (or something similar) that the compiler can use
> to
> determine what computer the program is being compiled on?
>
>
> Something like:
>
>
> [assume an environment variable called COMPUTER_NAME which is =
> "COMPUTER1"
> or "COMPUTER2"]
>
>
> #ifdef GET_ENVIRONMENT_VAR(COMPUTER_NAME)
>
>
> ... "GET_ENVIRONMENT_VAR()" translates to "COMPUTER1" or "COMPUTER2"
>
>
> ...
>
>
> ?
>
> --
> Arthur


Nov 17 '05 #5

P: n/a
Arthur wrote:
I had two Visual Studio .NET C++ solutions that I combined into one. These
project solutions were very similar. In fact, the two solutions were sharing
many files.
Now that they are one solution, I #ifdef'd pre-processor variables
throughout the code that will compile the code differently. The solution
needs to be #define'd differently depending on which computer I compile it
on:
#define COMPUTER1
//#define COMPUTER2 // comment out above and uncomment this when
compiling on COMPUTER2

#ifdef COMPUTER1
... code specific for COMPUTER1
#endif
#ifdef COMPUTER2
.. code specific for COMPUTER2
#endif
My question: Instead of having a "#define COMPUTER1", how can I use an
environment variable (or something similar) that the compiler can use to
determine what computer the program is being compiled on?


...[stuff deleted]...

Why not create two projects in a single solution. One has COMPUTER1 defined and
the other has COMPUTER2?

--
Steve Alpert
my email Fgrir_Nycreg @ vqk.pbz is encrypted with ROT13 (www.rot13.org) and spaces

Nov 17 '05 #6

P: n/a
Arthur wrote:
I had two Visual Studio .NET C++ solutions that I combined into one.
These project solutions were very similar. In fact, the two
solutions were sharing many files. #define COMPUTER1
//#define COMPUTER2 // comment out above and uncomment this when
compiling on COMPUTER2


As another poster suggested, use the /D switch [or add COMPUTER1 to the
Preprocessor defines list in the IDE]. Do not define them in your source
files as you have shown above.

Now, create another *configuration* with settings copied from the existing
one (May be one each for your debug and release builds). In the new
configuration, change the preprocessor definition COMPUTER1 to COMPUTER2.

You now have two different builds from the same source tree. Build the one
you need (or both using the batch build option).

Best,
Sarat Venugopal

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.