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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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"


....


?
 
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).
 
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 said:
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 said:
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 wrote:
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
 
You can use the CL environment variable. See
http://msdn2.microsoft.com/en-us/library/kezkeayy.aspx

--
Regards,
Nish [VC++ MVP]


Arthur said:
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 said:
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 said:
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 said:
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?
 
Arthur said:
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
 
Back
Top