473,378 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
6 4425
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: alwayswinter | last post by:
I currently have a form where a user can enter results from a genetic test. I also have a pool of summaries that would correspond to different results that a user would enter into the form. I...
10
by: SueB | last post by:
I currently have a 'mail-merge' process in my Access db project. It generates custom filled out Award Certificates based on an SQL SELECT statement in a VBA routine invoked by clicking on a...
4
by: Michel Racicot | last post by:
Ok, I heard that C# code was supposed to run on many platforms/OS, as long as you have the CLR installed. Where can I download the CLR for other OS? Thank you
3
by: Nevyn Twyll | last post by:
I have a bunch of test case classes (.cs) files in my executable. For security and bloat reasons, I don't want them to compile into a Release executable - only into a Debug build. How do I...
3
by: | last post by:
One thing I did a lot of in Classic ASP involved showing page elements conditionally based on whether a user was logged in or not. Logged in users or "superusers" would get more content and/or more...
2
by: Craig | last post by:
Is there a way to conditionally include a file in the HTML? <TD> if x= 1 then <!--#INCLUDE FILE="../File1"--> else <!--#INCLUDE FILE="../File2"--> end if
2
by: Felix | last post by:
Hello, I use a #define like WITH_MYSTUFF to conditionally compile code within #ifdef WITH_MYSTUFF. I would now like the same define to conditionally link with a library. In the linker settings...
1
by: kaens | last post by:
So, I have a class that has to retrieve some data from either xml or an sql database. This isn't a problem, but I was thinking "hey, it would be cool if I could just not define the functions for...
3
by: yasmin | last post by:
I am trying to conditionally compile macros in my c++ program. I want to simply be able to use the macro name without enclosing it in #ifdef-#endif each time. How can I enclose the macro definition...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.