Distinguish OS at compile time

J

JCR

Hello,

I need to write somethign like
if WINDOWS
do that
if MAC
do this

Any Macro that can do the job? Should it be compiler specific?

Many thanks
 
J

Jack Klein

Hello,

I need to write somethign like
if WINDOWS
do that
if MAC
do this

Any Macro that can do the job? Should it be compiler specific?

Many thanks

Consult the documentation for the compilers involved, or ask in
support groups for the specific compilers. This is not a C++ language
question.
 
R

Robbie Hatley

JCR said:
I need to write somethign like
if WINDOWS
do that
if MAC
do this


In that case, I suggest you write something like:


#if defined WINDOWS

// do thing one

#elif defined MAC

// do thing two

#elif defined OS_370

// do thing three

#else

// unsupported OS; do nothing

#endif

Any Macro that can do the job?

A macro, by itself, is just one piece of text which is
replaced with another by the preprocessor. To get the above
scheme to work, you'll have to define "WINDOWS", "MAC", etc.
in your compiler's settings for the various configurations for
your program. In Visual Studio, use the per-configuration
project settings to do this. ("Preprocessor" tab.) For other
compilers, consult your manual.
Should it be compiler specific?

Macros are not compiler specific, no. Their behavior is defined
by the C and C++ standards. For more info, read ISO/IEC-14882.
You can buy a copy from ANSI over the web for $30:
http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS/ISO/IEC+14882-2003
Look in chapter 16, "Preprocessing Directives".

Details of how your compiler USES macros are compiler-specific,
though. Read your compiler's manual for more.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
R

Robbie Hatley

Jack Klein said:
Consult the documentation for the compilers involved, or ask in
support groups for the specific compilers. This is not a C++ language
question.


Sorry, but that's just wrong. Macros and conditional compilation
are not compiler-specific. They're defined by the C++ standard.
Read IEC/ISO-14882 chapter 16, "Preprocessor Directives". Pay
especial especial attention to section 16.1, "Conditional Inclusion".

Let's not throw the baby out with the bathwater when it comes to
rejecting off-topic posts. Some that may seem off-topic at first
glance are actually very ON-topic if you think about them for a
few more seconds.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
A

Alf P. Steinbach

* JCR:
Hello,

I need to write somethign like
if WINDOWS
do that
if MAC
do this

Any Macro that can do the job? Should it be compiler specific?

In addition to Robbie Hatley's replies, see <url:
http://predef.sourceforge.net/preos.html> for a list of such macros.

Also, an alternative and often better scheme is to provide platform
specific implementation files, place them in their own per-platform
directory, and select the relevant one at build-time or even
installation time. This moves the issue from being a language issue to
being a build-tools or installation-tools issue, and provides more clean
and maintainable code. However, that doesn't work nearly so well with
compiler-specific code, because you don't want to duplicate essentially
the same code (which would reduce instead of increase maintainability).

Thus, for compiler-specific code some macros like those above can be
necessary, although their effect should (ideally) be centralized, not
peppered around in source files; relevant compiler-detection macros are
listed at <url: http://predef.sourceforge.net/precomp.html>.
 
J

James Bannon

JCR said:
Hello,

I need to write somethign like
if WINDOWS
do that
if MAC
do this

Any Macro that can do the job? Should it be compiler specific?

Many thanks

The bottom line is "don't do this!" it's a maintenance nightmare and
it's evil! Rather than making your code portable, which I assume is the
aim here, it tightly couples it to the architectures you happen to
define. What happens if you, or someone else, wants to move the code to
a different architecture? Besides this, it is inevitable that the
different code for the different architectures will diverge over time.

Develop an architecture independent interface first and then plug-in
architecture dependent code that obeys the interface conventions.
Something like this:

T f ( args )
{
// common code
g (interface args);
return result;
}

g (interface args)
{ // MAC version
....
}

g (interface args)
{ // Windows version
.....
}

......

Save the different versions in directories called <arch> and then use
the build system to select the version of g (...) you want.

Believe you me as someone who had to hack code like this to get it to
work on different platforms, the maintainers will not give you a vote of
thanks for having to wade through a tortured path of ifdefs when trying
to find bugs in the code. If you are the only maintainer then it will
make your job easier too.

Another strategy is to develop for only one architecture but still use
the same separation of concerns to isolate the architecture-dependent
parts of the code. That way you can isolate bugs to some dependent
routines only defined in a single place making them easier to check.

Cheers
Jim.
 
J

John Carson

Robbie Hatley said:
In that case, I suggest you write something like:


#if defined WINDOWS

// do thing one

#elif defined MAC

// do thing two

#elif defined OS_370

// do thing three

#else

// unsupported OS; do nothing

#endif



A macro, by itself, is just one piece of text which is
replaced with another by the preprocessor. To get the above
scheme to work, you'll have to define "WINDOWS", "MAC", etc.
in your compiler's settings for the various configurations for
your program. In Visual Studio, use the per-configuration
project settings to do this. ("Preprocessor" tab.) For other
compilers, consult your manual.


Macros are not compiler specific, no. Their behavior is defined
by the C and C++ standards. For more info, read ISO/IEC-14882.
You can buy a copy from ANSI over the web for $30:
http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS/ISO/IEC+14882-2003
Look in chapter 16, "Preprocessing Directives".

Details of how your compiler USES macros are compiler-specific,
though. Read your compiler's manual for more.

Lots of macros are indeed compiler specific and can be used as a basis for
identifying what compiler is being used. For VC++, see the following
extensive list:

http://msdn2.microsoft.com/en-us/library/b0084kay.aspx

Note especially, _WIN32, _WIN64 and _MSC_VER.
 
S

sr.sakhamuri

James said:
The bottom line is "don't do this!" it's a maintenance nightmare and
it's evil! Rather than making your code portable, which I assume is the
aim here, it tightly couples it to the architectures you happen to
define. What happens if you, or someone else, wants to move the code to
a different architecture? Besides this, it is inevitable that the
different code for the different architectures will diverge over time.

Develop an architecture independent interface first and then plug-in
architecture dependent code that obeys the interface conventions.
Something like this:

T f ( args )
{
// common code
g (interface args);
return result;
}

g (interface args)
{ // MAC version
....
}

g (interface args)
{ // Windows version
.....
}

I think the Abstract Factory pattern can be used here, to load the
dependent code with a hint about architecture from a config file
 
J

Jack Klein

Sorry, but that's just wrong. Macros and conditional compilation
are not compiler-specific. They're defined by the C++ standard.
Read IEC/ISO-14882 chapter 16, "Preprocessor Directives". Pay
especial especial attention to section 16.1, "Conditional Inclusion".

Let's not throw the baby out with the bathwater when it comes to
rejecting off-topic posts. Some that may seem off-topic at first
glance are actually very ON-topic if you think about them for a
few more seconds.

No, you are wrong. Indeed, the C++ language standard does define
macros and conditional inclusion. It also lists a number of macros
that a conforming implementation is required to define. Absolutely
none of them provide any indication whatsoever of the operating
system.

On the other hand, just about every specific compiler does provide
some additional macros of its own, that identify the compiler, its
version, and often the target platform. The information on these
macros is provided in the compiler's documentation.
 
R

Robbie Hatley

Jack Klein said:
No, you are wrong.

No, my statement was not "wrong". My statement was correct.
Your statement, on the otherhand, was incorrect.
You admit as much here:
Indeed, the C++ language standard does define macros and
conditional inclusion.

Which is exactly what I said.
It also lists a number of macros that a conforming implementation
is required to define. Absolutely none of them provide any
indication whatsoever of the operating system.

True, but not relevant to your previous (incorrect) statement that
macros and conditional inclusion are "not a C++ language question".
On the other hand, just about every specific compiler does provide
some additional macros of its own, that identify the compiler, its
version, and often the target platform. The information on these
macros is provided in the compiler's documentation.

True, but not relevant to your previous (incorrect) statement that
macros and conditional inclusion are "not a C++ language question".

Bottom line: you made a mistake. Why not just admit it and say
"oops, my mistake"? It's no disgrace. Obfuscating with
irrelevancies, on the otherhand, is silly and counterproductive.

Furthermore, it's not good to tell posters here to go elsewhere just
because some of their questions are platform-specific or compiler-
specific. It's better to actually answer the on-topic C++ questions,
then mention some resources the poster can access to resolve their
off-topic questions. That's much more helpful than just saying
"Y'r off-topic! Go away!" After all, that's what this group is
about: people helping people with C++.

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
K

Kai-Uwe Bux

Robbie said:
No, my statement was not "wrong". My statement was correct.
Your statement, on the otherhand, was incorrect.
You admit as much here:


Which is exactly what I said.

But you said more, you also said the OP was on-topic.
True, but not relevant to your previous (incorrect) statement that
macros and conditional inclusion are "not a C++ language question".


True, but not relevant to your previous (incorrect) statement that
macros and conditional inclusion are "not a C++ language question".

Actually, the question of the OP was:
I need to write somethign like
if WINDOWS
do that
if MAC
do this

Any Macro that can do the job?

He was *not* asking about macros in general, but whether there is a
particular macro "that can do the job". That question of the OP is not
covered by the standard, as was correctly pointed out. You are
misrepresenting the issue at hand by reading the OP's question in a way to
general way.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top