Topic: Standard library headers


Author: David Bourguignon <David.Bourguignon@imag.fr>
Date: 2000/03/01
Raw View
Hi everybody,

As a beginner, I need a piece of advice concerning portability of the
std lib headers.

Let's take an example. I use two different compilers:
- on one hand, CC (SGI MIPSpro compiler) provides the std lib <limits>
header but do not provide the <cmath> one ;
- on the other hand, g++ doesn't provide <limits> but do provide
<cmath>.

What is the best strategy to adopt when someone want to write portable
code with #include of the std lib headers? Just forget it and only use
good old C <xxx.h> headers?

I thank you in advance for you answer.


        David.

---
David Bourguignon
iMAGIS project, GRAVIR lab
INRIA Rh   ne-Alpes, 655 avenue de l'Europe
38330 Montbonnot Saint Martin
France

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul Lutus" <nospam@nosite.com>
Date: 2000/03/01
Raw View
The usual remedy is to use #ifdef blocks. Assume there is a unique
definition in compiler B only, named FLAGB.

#ifdef FLAGB

// headers for compiler B

#else

// headers for compiler A

#endif

One hopes it is possible to write the code itself to accommodate the
differences between these two environments.

And "portable code" in this case is a stretch. Obviously and eventually,
some compiler C will come along and ruin this scheme, which is why it is not
a good idea unless absolutely necessary.

Think of this as an interim solution, to be used only until all your
compilers are up-to-date.

--

Paul Lutus
www.arachnoid.com


David Bourguignon <David.Bourguignon@imag.fr> wrote in message
news:38BBBE72.26EDAFE0@imag.fr...
> Hi everybody,
>
> As a beginner, I need a piece of advice concerning portability of the
> std lib headers.
>
> Let's take an example. I use two different compilers:
> - on one hand, CC (SGI MIPSpro compiler) provides the std lib <limits>
> header but do not provide the <cmath> one ;
> - on the other hand, g++ doesn't provide <limits> but do provide
> <cmath>.
>
> What is the best strategy to adopt when someone want to write portable
> code with #include of the std lib headers? Just forget it and only use
> good old C <xxx.h> headers?
>
> I thank you in advance for you answer.
>
>
>         David.
>
> ---
> David Bourguignon
> iMAGIS project, GRAVIR lab
> INRIA Rh   ne-Alpes, 655 avenue de l'Europe
> 38330 Montbonnot Saint Martin
> France
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: N J Chackowsky <chackowsky@BrandonSD.MB.CA>
Date: 2000/03/01
Raw View
Egads, my first *post* to this group. David--DJGPP provides <climits>,
so I assume g++ would do the same. I realize this solves only part of
your problem. If namespace polution is not an issue, you could include
<math.h> and <climits> with both compilers, I think???

Nick.



David Bourguignon wrote:
>
> Hi everybody,
>
> As a beginner, I need a piece of advice concerning portability of the
> std lib headers.
>
> Let's take an example. I use two different compilers:
> - on one hand, CC (SGI MIPSpro compiler) provides the std lib <limits>
> header but do not provide the <cmath> one ;
> - on the other hand, g++ doesn't provide <limits> but do provide
> <cmath>.
>
> What is the best strategy to adopt when someone want to write portable
> code with #include of the std lib headers? Just forget it and only use
> good old C <xxx.h> headers?
>
> I thank you in advance for you answer.
>
>         David.
>
> ---
> David Bourguignon
> iMAGIS project, GRAVIR lab
> INRIA Rhtne-Alpes, 655 avenue de l'Europe
> 38330 Montbonnot Saint Martin
> France
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Matt Austern <austern@sgi.com>
Date: 2000/03/01
Raw View
David Bourguignon <David.Bourguignon@imag.fr> writes:

> Hi everybody,
>
> As a beginner, I need a piece of advice concerning portability of the
> std lib headers.
>
> Let's take an example. I use two different compilers:
> - on one hand, CC (SGI MIPSpro compiler) provides the std lib <limits>
> header but do not provide the <cmath> one ;
> - on the other hand, g++ doesn't provide <limits> but do provide
> <cmath>.
>
> What is the best strategy to adopt when someone want to write portable
> code with #include of the std lib headers? Just forget it and only use
> good old C <xxx.h> headers?

Well, one piece of advice is that you should complain to your compiler
vendors and ask them to provide exactly what the standard tells them
to.  (Yes, I did read what you said, and I realize I'm suggesting that
you complain to me!)

In the near term, though, you probably will be more portable if you
use the old <foo.h> version of the C library headers than if you use
the new <cfoo> version.  As you've seen, there are some compilers that
just don't provide the new <cfoo> version.  There are also some
compilers, even some popular compilers, that provide the new <cfoo>
version but that get it wrong.  If you use the new-style C library
headers you'll have to cope with the compilers that get the new-
style headers wrong.

The standard is quite explicit about what those headers are supposed
to look like: they are supposed to define C library names in namespace
std, and, unless you include the <foo.h> version, they are not
supposed to bring those names into the global namespace.  That is,
this program is supposed to work:
    #include <cstdio>
    int main() {
      std::printf("Hello, world!\n");
    }
But this program is an error, and a diagnostic is required:
    #include <cstdio>
    int main() {
      printf("Hello, world!\n");  // Error, ::printf is undefined
    }

Some compilers incorrectly define <cstdio> so that it puts printf in
the global namespace.

Please note: I'm talking only about the old versus new versions of the
C library headers.  I'm not talking about headers, like <vector> and
<locale>, that contain C++ library facilities.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/03/01
Raw View
David Bourguignon wrote:
>
> As a beginner, I need a piece of advice concerning portability of the
> std lib headers.
>
> Let's take an example. I use two different compilers:
> - on one hand, CC (SGI MIPSpro compiler) provides the std lib <limits>
> header but do not provide the <cmath> one ;
> - on the other hand, g++ doesn't provide <limits> but do provide
> <cmath>.

The <limits> header is specific to C++. The <limits.h> header is inherited
from the C library and has completely different content.  The related C++
header is <climits>, not <limits>.

>
> What is the best strategy to adopt when someone want to write portable
> code with #include of the std lib headers? Just forget it and only use
> good old C <xxx.h> headers?

C++ inherits the 18 standard C headers in their <xxx.h> form. The C++
standard also specifies alternative <cxxx> versions. The latter versions
put all the standard declarations only in the "std" namespace. The <xxx.h>
versions typically contain declarations other than those specified by
the C and C++ standards. (For example, POSIX and XOpen specify
additional content for some of those headers, and various operating
systems typically add OS-specific declarations for historical reasons.)
Whether these additional declarations appear in the <cxxx> headers, and
whether those declarations are in namespace std, depends on the
implementation.

The C++ standard also specifies 32 additional headers that are unique to
C++. These include what is informally known as the "STL", string classes,
iostream classes, and miscellaneous runtime support declarations. These
standard C++ headers do not have a <xxx.h> form. Some C++ implementations
provide .h versions of some of those 32 headers, but you can't depend
on any particular content (because they are not standard).

Since not all C++ implementations provide the <cxxx> versions of the
18 C headers, your code will be more portable if you use the <xxx.h>
forms. For the remaining 32 C++ headers, use the <xxx> form for
portability. If the implementation provides the C++ standard library
at all, it will provide the <xxx> headers.

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Barry Wallis" <me@mine.com>
Date: 2000/03/02
Raw View
Matt is too modest. :-) VC++ is one compiler that leaves the C functions in
the global namespace when included using <cxxx>


--


Barry L. Wallis, Senior Systems Engineer
Science Applications International Corporation
4161 Campus Point Court
San Diego, CA 92121
  _____
"In all labor there is profit, but mere talk leads to poverty."
- King Solomon

"Matt Austern" <austern@sgi.com> wrote in message
news:fxt4sarvpp1.fsf@isolde.engr.sgi.com...
> David Bourguignon <David.Bourguignon@imag.fr> writes:
>
> > Hi everybody,
> >
> > As a beginner, I need a piece of advice concerning portability of the
> > std lib headers.
> >
> > Let's take an example. I use two different compilers:
> > - on one hand, CC (SGI MIPSpro compiler) provides the std lib <limits>
> > header but do not provide the <cmath> one ;
> > - on the other hand, g++ doesn't provide <limits> but do provide
> > <cmath>.
> >
> > What is the best strategy to adopt when someone want to write portable
> > code with #include of the std lib headers? Just forget it and only use
> > good old C <xxx.h> headers?
>
> Well, one piece of advice is that you should complain to your compiler
> vendors and ask them to provide exactly what the standard tells them
> to.  (Yes, I did read what you said, and I realize I'm suggesting that
> you complain to me!)
>
> In the near term, though, you probably will be more portable if you
> use the old <foo.h> version of the C library headers than if you use
> the new <cfoo> version.  As you've seen, there are some compilers that
> just don't provide the new <cfoo> version.  There are also some
> compilers, even some popular compilers, that provide the new <cfoo>
> version but that get it wrong.  If you use the new-style C library
> headers you'll have to cope with the compilers that get the new-
> style headers wrong.
>
> The standard is quite explicit about what those headers are supposed
> to look like: they are supposed to define C library names in namespace
> std, and, unless you include the <foo.h> version, they are not
> supposed to bring those names into the global namespace.  That is,
> this program is supposed to work:
>     #include <cstdio>
>     int main() {
>       std::printf("Hello, world!\n");
>     }
> But this program is an error, and a diagnostic is required:
>     #include <cstdio>
>     int main() {
>       printf("Hello, world!\n");  // Error, ::printf is undefined
>     }
>
> Some compilers incorrectly define <cstdio> so that it puts printf in
> the global namespace.
>
> Please note: I'm talking only about the old versus new versions of the
> C library headers.  I'm not talking about headers, like <vector> and
> <locale>, that contain C++ library facilities.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David Bourguignon <David.Bourguignon@imag.fr>
Date: 2000/03/02
Raw View
Matt Austern wrote:
>
> Well, one piece of advice is that you should complain to your compiler
> vendors and ask them to provide exactly what the standard tells them
> to.  (Yes, I did read what you said, and I realize I'm suggesting that
> you complain to me!)
>

Hi,

Thank you for your answer.
Do you know why CC is not providing <cmath>, for example? Is there a
performance/portability issue for IRIX behind this? Or something else ?

As a temporary solution, I will simply use <math.h> and the <xxx.h>
headers.

        David.



P.S.: Thanks also to the others who have helped me !


---
David BOURGUIGNON
iMAGIS project, GRAVIR lab
INRIA Rh   ne-Alpes, 655 avenue de l'Europe
38330 Montbonnot Saint Martin, France

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]