preprocessing statements

B

Bill Cunningham

Keith Thompson said:
One more thing that you didn't ask about (but probably should have):

How do you expect the symbol LINUX to be defined? Since that
identifier must be available for use in programs, an implementation is
not allowed to pre-define it. There may be other symbols that are
predefined (if you're trying to determine whether you're on a Linux
system); which symbols those might be is a question for another forum.

If you're defining it yourself, that's fine, but you didn't show that.

Also, you appear to be trying to write code that will use things
declared in those implementation-defined headers if you're on a Linux
system, but will still compile and work properly otherwise. Such
things are doable, but the nature of the questions you've been asking
here suggest to me that this is a more advanced topic than you're
ready for.
Perhaps so but the code above didn't work. This code I wrote did. The
question is did I do it correctly.

#include <stdio.h>
#include <stdlib.h>
#ifdef linux
#ifndef linux
#undef linux
#include <unistd.h>
#include <sys/types.h>
#include <sys/stats.h>
#include <fctnl.h>
#endif
#endif

I compiled a simple program asking sizeof to show through printf the
size of a long double. I haven't defined linux to use those sys call headers
yet though so that part of the header is untested. But the directives
blocked out the mentioned OT headers to compile a simple C program. I
must've did something right.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Perhaps so but the code above didn't work. This code I wrote did. The
question is did I do it correctly.

#include <stdio.h>
#include <stdlib.h>
#ifdef linux
#ifndef linux
#undef linux
#include <unistd.h>
#include <sys/types.h>
#include <sys/stats.h>
#include <fctnl.h>
#endif
#endif

I compiled a simple program asking sizeof to show through printf the
size of a long double. I haven't defined linux to use those sys call headers
yet though so that part of the header is untested. But the directives
blocked out the mentioned OT headers to compile a simple C program. I
must've did something right.

Yes, it should work; it just doesn't make any sense.

You have a "#ifndef linux" nested within a "#ifdef linux". What is
this supposed to accomplish? What is the purpose of the "#undef
linux"?

Under what circumstances, if any, do you expect the symbol "linux" or
"LINUX" to be defined? Do you intend to define it yourself?

Do you intend to write code that uses things declared in those
headers? Is your program going to *depend* on those features?

Based on what you've told us so far, a much simpler solution would be
to omit the #include directives for the system-specific headers,
leaving just this:

#include <stdio.h>
#include <stdlib.h>

What exactly are you trying to accomplish?
 
B

Bill Cunningham

Yes, it should work; it just doesn't make any sense.

You have a "#ifndef linux" nested within a "#ifdef linux". What is
this supposed to accomplish? What is the purpose of the "#undef
linux"?

Under what circumstances, if any, do you expect the symbol "linux" or
"LINUX" to be defined? Do you intend to define it yourself?

Do you intend to write code that uses things declared in those
headers? Is your program going to *depend* on those features?

Yes I use them too. But they are OT here. I want to write something like
this.

#include "master.h"
#define linux

Then use those non-standard headers with stdio.h and stdlib.h. Otherwise if
linux is not defined.

#include "master.h"

int main(void) {

C code.

}
 
K

Keith Thompson

Bill Cunningham said:
Yes I use them too. But they are OT here. I want to write something like
this.

#include "master.h"
#define linux

Then use those non-standard headers with stdio.h and stdlib.h. Otherwise if
linux is not defined.

#include "master.h"

int main(void) {

C code.

}

Based on what you've told us so far, I'm unable to figure out what
you're trying to do. I asked you several specific questions; you've
answered almost none of them. Rather than telling us what you're
trying to accomplish, you've shown us some pseudo-code of what I
presume is intended to be a solution.

What problem are you trying to solve?
 
B

Bill Cunningham

Keith Thompson said:
What problem are you trying to solve?
In all compilation files include stdio.h and stdlib. When linux is
defined include several non-standard headers at compile time.

Bill
 
K

Keith Thompson

Bill Cunningham said:
In all compilation files include stdio.h and stdlib. When linux is
defined include several non-standard headers at compile time.

No, that's your proposed *solution*. What *problem* are you trying to
solve? How is your program supposed to behave?
 
B

Bill Cunningham

No, that's your proposed *solution*. What *problem* are you trying to
solve? How is your program supposed to behave?

I want one header called master.h that includes all the headers that
have been typed. I am tired of typing them all in one at a time. I wish to
have a header with conditional compilation.

Bill
 
V

vippstar

I want one header called master.h that includes all the headers that
have been typed. I am tired of typing them all in one at a time.
Use a proper text editor that does this for you.
clc is not a group to ask about text editors.
 
K

Keith Thompson

William Pursell said:
There is a problem with the approach you are taking that should be
considered. It is not really adequate to
use the simple pre-processor check of '#ifdef LINUX'. It would
be more appropriate to do something like: (caveat: it
is IMO totally inappropriate to be doing this at all, but
if you really want to, the following approach will be more
robust)

#ifdef HAVE_SYS_FOO
#include <sys/foo.h>
#endif

The fact that the particular Linux you are using today has
sys/foo.h is not sufficient, and neither you nor the
preprocessor really cares if the system is Linux or not...
only whether or not sys/foo.h can be included. Check for
features, not for the system.

Except that you just might care whether you have the the Linux
sys/foo.h or the Solaris sys/foo.h.
 
B

Ben Bacarisse

Bill Cunningham said:
huh??

Have you read this thread?

It is likely he has. He is saying that using conditional compilation
and a big master.h header is not the best way to solve the problem of
being "tired typing all the includes". My editor lets me type #incude
<stdio.h> using just a few keys. I can "pull in" an outline C program
to write quick test in just a couple of key strokes[1].

If you go your way, you won't really have a portable programs. There
are too many small differences between in ways systems handle the bit
outside of the C standard.

[1] But since it is Emacs most of my fingers are involved in each
stroke!
 
B

Bill Cunningham

It is likely he has. He is saying that using conditional compilation
and a big master.h header is not the best way to solve the problem of
being "tired typing all the includes". My editor lets me type #incude
<stdio.h> using just a few keys. I can "pull in" an outline C program
to write quick test in just a couple of key strokes[1].

If you go your way, you won't really have a portable programs. There
are too many small differences between in ways systems handle the bit
outside of the C standard.

[1] But since it is Emacs most of my fingers are involved in each
stroke!
I see ok well based on earlier posts in this thread perhaps I
misunderstood. Text editors had not been mentioned to that point in the
thread and I understand they are OT. Unless someone was writing one and they
mentioned their code itself. Then editors would still be OT just not C code.
:)
I was going to use conditional compilation and all these headers as a
learning tool for myself. So I wasn't looking so much at the importance of
portability in the program. The using of non-standard headers itself would
destroy portability.

Bill
 
B

Bill Cunningham

Except that you just might care whether you have the the Linux
sys/foo.h or the Solaris sys/foo.h.
I only wanted to do this conditional compilation thing as a personal
learning tool for preprocessor directives. Even using non-standard headers
destroys portability which is all important we all understand. It was to
learn and save me keystrokes.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I only wanted to do this conditional compilation thing as a personal
learning tool for preprocessor directives. Even using non-standard headers
destroys portability which is all important we all understand. It was to
learn and save me keystrokes.

Saving you keystrokes isn't an issue as long as copy-and-paste exists.

As for portability, there are 3 classes of programs. (I don't claim
this is exhaustive or definitive, just relevant to what you're doing.)

1. Portable programs that depend only on standard headers.

2. Non-portable programs that depend on non-standard headers; these
will compile and run only on a particular system (e.g., Linux).

3. Programs that are a mixture of the above. A program might
conditionally include one or more system-specific headers. Any code
that uses features defined in those headers would itself have to be
conditionalized (surrounded by #if ... #endif or #ifdef ... #endif).
For example, a program might provide a textual interface on all
systems, and additionally provide a GUI only on systems where it's
available.

The stuff you're trying to do:

#include <stdio.h>
#ifdef linux
#include <sys/foo.h>
#endif

makes sense for the third class of programs, but I don't believe
that's what you're trying to do -- and if it is, I suggest that you're
in over your head, given the difficulties you've had mastering the
basic concepts of portable standard C.

Personally, I'm not a big fan of these "everything.h" headers that
just include all the other headers you might or might not need. If a
translation unit uses printf(), then *that* source file should have
"#include <stdio.h>", and likewise for any other standard or
non-standard headers.

Incidentally, portability isn't "all important". It's one among many
important attributes that a program can have. If you need to do
something system-specific, go ahead and do it; your program isn't
going to be portable to other systems, but it doesn't need to be.

Non-portable code isn't necessarily bad, it's just off-topic here,
because we try to discuss portable aspects of the language as defined
by thes standard. If you have Unix-specific questions, for example,
you can ask them in comp.unix.programmer.
 
K

Keith Thompson

Richard Heathfield said:
[I know I'm preaching to the choir here, but maybe some non-choristers will
drop by and pick up a few pearls of what I am pleased to call "wisdom".]

Keith Thompson said:

Incidentally, portability isn't "all important". It's one among many
important attributes that a program can have.
Right...

If you need to do
something system-specific, go ahead and do it; your program isn't
going to be portable to other systems, but it doesn't need to be.

...and less right. <g>

We can't tell whether a program needs to be portable purely on the basis of
whether we need it to do something system-specific. Rather, what we have
here is two conflicting characteristics:

(a) portability, and
(b) system-specific features.

Agreed. My phrase "If you *need* to do something system-specific" was
intended to cover that; thank you for saying more clearly than I did
that it's a tradeoff.

[snip lots of good stuff]
True, but I think it's reasonable to consider as topical the ways in which
we might isolate SSF usage, in ISO C.

Agreed.
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top