#else not working

H

hobbes_7_8

Hi everybody!

I'm having a very strange behaviour on my pre-processor. Basically I
have the following piece of code:

#if (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 4)) \
|| defined(_POSIX2_C_VERSION) \
|| (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L))
#include <unistd.h>
#else
extern "C"
{
#include "getopt.h"
}
#endif

.... the goal is to use the system's implementation of the getopt()
function if it is available (that's why those tests are there) or use
the sample implementation that came with Visual Studio 6.0 in the
remaining cases (e.g., on Windows).

On Windows 2000 + Visual Studio .NET 2003, everything works fine.

On HP-UX B.11.11 U + gcc 3.4.2, I get the following error:

In file included from effdoc.cpp:3:
precomp.h:29:20: getopt.h: No such file or directory
*** Error exit code 1

Stop

.... this is very strange as the pre-processor macros tested should make
the contents of the if clause expand, not the else clause:

_XOPEN_VERSION = 4
_POSIX_VERSION = 199309
_POSIX2_C_VERSION = 199209

.... does anyone have an idea of what's going on?

Any help greatly appreciated.

André
 
V

Victor Bazarov

I'm having a very strange behaviour on my pre-processor. Basically I
have the following piece of code:

#if (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 4)) \#include <unistd.h>
#else
extern "C"
{
#include "getopt.h"
}
#endif

[..]

On HP-UX B.11.11 U + gcc 3.4.2, I get the following error:

In file included from effdoc.cpp:3:
precomp.h:29:20: getopt.h: No such file or directory
*** Error exit code 1

Stop

... this is very strange as the pre-processor macros tested should
make the contents of the if clause expand, not the else clause:

_XOPEN_VERSION = 4
_POSIX_VERSION = 199309
_POSIX2_C_VERSION = 199209

... does anyone have an idea of what's going on?

I bet HP Tech Support does.

V
 
P

Phlip

hobbes_7_8 said:
#if (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 4)) \
|| defined(_POSIX2_C_VERSION) \
|| (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L))

This is not a preprocessor issue, it's a "code-too-complex"
issue.

Break it up:

#define XOPEN (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 4))
#define POSIX2 (defined(_POSIX2_C_VERSION))
#define POSIX (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L))

Now you can use #warning XOPEN or similar to temporarily print out the
intermediate values.

Then put them back together:

#if XOPEN || POSIX2 || POSIX

Much more readable.
 
H

hobbes_7_8

Actually, the fact that this is running on an HP-UX system only
indirectly makes those pre-processor macros have the values they have.
And that's it.

This is a problem regarding the behaviour of the pre-processor, which
is part of gcc. Therefore HP support is not the one to be called.
 
H

Howard

Actually, the fact that this is running on an HP-UX system only
indirectly makes those pre-processor macros have the values they have.
And that's it.

And that's...what?
This is a problem regarding the behaviour of the pre-processor, which
is part of gcc. Therefore HP support is not the one to be called.

_What's_ a problem with the pre-processor?

It would help if you quoted what you're replying to.

Do you still need help? If so, you might take the suggestion from Philip
and break down those #defines into smaller pieces. You might then also want
to take those defined values and check them individually (such as with their
own #ifdef sections, wrapping some kind of statements which you can easily
recognize, such as error or warning statements). Then you can test exactly
what's going on. It just might be that your assumption about one or more of
those values is incorrect.

(I'm also wondering: do those \ line continuation markers apply to #if
statements? They might be causing the problem. Just a thought.)

-Howard
 
O

Old Wolf

Hi everybody!

I'm having a very strange behaviour on my pre-processor. Basically I
have the following piece of code:

#if (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 4)) \
|| defined(_POSIX2_C_VERSION) \
|| (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L))
#include <unistd.h>
#else
extern "C"
{
#include "getopt.h"
}
#endif

In file included from effdoc.cpp:3:
precomp.h:29:20: getopt.h: No such file or directory
*** Error exit code 1

Are you sure that the #else is being executed? Perhaps unistd.h
includes getopt.h .

Supposing it is: are you sure that those macros are actually
defined at this point? What headers did you include previously
to this #if statement? You might like to use other #ifdef tests
to check if the macros are actually defined as you think they are.
 
H

hobbes_7_8

Hi everybody!

I found what the problem was. Basically I was testing pre-processor
macros in order to determine if I should include unistd.h. Well, it
happens those macros were defined in unistd.h itself :S

I had to make an awkward statement (see below), but the fact is that it
now works.

Thanks for the tips, anyway!

André
--
#if defined(unix) || defined(__unix) || defined(__unix__)
#include <unistd.h>
#endif
#if (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 4)) \
|| defined(_POSIX2_C_VERSION) \
|| (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L))
// getopt was added to <unistd.h> in XPG4 and POSIX 1003.2
#else
extern "C"
{
#include "getopt.h"
}
#endif
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top