Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > header file inclusion in c++

Reply
Thread Tools

header file inclusion in c++

 
 
Rahul
Guest
Posts: n/a
 
      12-23-2007
Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

Thanks in advance!!!
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      12-23-2007
"Rahul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I often include the header files like the following,
>
> #include <string.h>
>
> However, i get a warning from the compiler to remove the .h and it
> works fine with the following,
>
> #include <string>
>
> I also read an article which suggests to do the same, however it
> doesn't claim any reason to do so... does anyone have any idea
> regarding the same?


The C++ standard library includes the (C90) standard C library,
so headers from both languages are available. The C++ library also
provides 'new' forms of the C headers, with the same names prepended
with letter 'c', and the '.h' dropped. The original names (with .h)
are also included for compatibility.

So. e.g. you can #include <stdio.h> or <cstdio> for the same features.
Also the C names are encapsulated in namespace 'std' just as are the
C++ names (except macros).

Note that <string.h> (and <cstring>) declare the 'C-style' string
operations (e.g. strcpy(), and <string> declares the C++ std::string type
and associated functions. Not the same thing at all.

Your questions cause me to ask which book(s) you're using to learn
C++ that don't explain this.

-Mike


 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      12-23-2007
On 2007-12-23 19:07, Mike Wahler wrote:
> "Rahul" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>>
>> I often include the header files like the following,
>>
>> #include <string.h>
>>
>> However, i get a warning from the compiler to remove the .h and it
>> works fine with the following,
>>
>> #include <string>
>>
>> I also read an article which suggests to do the same, however it
>> doesn't claim any reason to do so... does anyone have any idea
>> regarding the same?

>
> The C++ standard library includes the (C90) standard C library,
> so headers from both languages are available. The C++ library also
> provides 'new' forms of the C headers, with the same names prepended
> with letter 'c', and the '.h' dropped. The original names (with .h)
> are also included for compatibility.
>
> So. e.g. you can #include <stdio.h> or <cstdio> for the same features.
> Also the C names are encapsulated in namespace 'std' just as are the
> C++ names (except macros).


Just to clarify, if you include the C++version of a C header (e.g. one
of those starting with c and without the .h) the functions are placed in
the std namespace, if you include the C version of the header (those
with .h) they are in the global namespace.

Also, AFAIK, the C++ headers (those not also in C) have never ended in
..h in standard C++, however before C++ was standardised using .h was
common so many compiler supports it for backwards compatibility.

--
Erik Wikström
 
Reply With Quote
 
johanatan
Guest
Posts: n/a
 
      12-23-2007
On Dec 23, 1:07 pm, "Mike Wahler" <(E-Mail Removed)> wrote:

> Note that <string.h> (and <cstring>) declare the 'C-style' string
> operations (e.g. strcpy(), and <string> declares the C++ std::string type
> and associated functions. Not the same thing at all.
>


I think this point could use a little expansion. Since the OP was
asking about <string.h> vs. <string>, it should be pointed out that
this is an STL-specific requirement and the info provided about
standard C libs in C++, though useful, is slighty OT. Circa 1999, STL
dropped all the .h's from its include files and put them in an 'std'
namespace (std::vector, std::list, std::map, std::queue, etc.).

--Jonathan
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      12-23-2007
Rahul wrote:
> Hi,
>
> I often include the header files like the following,
>
> #include <string.h>
>
> However, i get a warning from the compiler to remove the .h and it
> works fine with the following,
>
> #include <string>
>
> I also read an article which suggests to do the same, however it
> doesn't claim any reason to do so... does anyone have any idea
> regarding the same?
>
> Thanks in advance!!!


As others are explaining, <string.h> is the c-style string functions. That
has been renamed to <cstring> in the latest version of c++. <string> is the
c++ style string STL std::string. However, <string> also includes some of
the c-style string functions also (not sure if it includes them all or not).

In either case, you would want to use <string> or <cstring>, but not the
depreciated <string.h>.

I'm just trying to point out that <string> is not the same as <string.h>,
but <cstring> is the same as <string.h> although you'll probably need to use
the std:: prefix.

--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
johanatan
Guest
Posts: n/a
 
      12-24-2007
On Dec 23, 5:48 pm, "Jim Langston" <(E-Mail Removed)> wrote:
> As others are explaining, <string.h> is the c-style string functions. That
> has been renamed to <cstring> in the latest version of c++. <string> is the
> c++ style string STL std::string. However, <string> also includes some of
> the c-style string functions also (not sure if it includes them all or not).
>


Well, I would assume that since his compiler said to replace
<string.h> with <string> (and presumably, the code still compiled)
that he was only using STL string functionality and the C-style
functions that might have been included as a side effect. So, the
most direct answer to his question:

> I also read an article which suggests to do the same, however it
> doesn't claim any reason to do so... does anyone have any idea
> regarding the same?


would be that STL dropped the .h's from its include files circa 1999.
This other C-style information that you guys have provided is just a
bonus IMHO.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-24-2007

"johanatan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Dec 23, 1:07 pm, "Mike Wahler" <(E-Mail Removed)> wrote:
>
>> Note that <string.h> (and <cstring>) declare the 'C-style' string
>> operations (e.g. strcpy(), and <string> declares the C++ std::string type
>> and associated functions. Not the same thing at all.
>>

>
> I think this point could use a little expansion. Since the OP was
> asking about <string.h> vs. <string>, it should be pointed out that
> this is an STL-specific requirement and the info provided about
> standard C libs in C++, though useful, is slighty OT.


The standard C headers (both those with '.h' and those without) are
defined by the standard, so they're certainly topical for comp.lang.c++.

> Circa 1999, STL
> dropped all the .h's from its include files


The first version of the C++ standard was dated 1998.
Note that the C++ standard does not define 'STL', but defines
a 'standard library' which includes many constructs from a
pre-standard entity commonly known as 'STL'. The C++ standard
has never defined any C++ library headers whose names contained
a '.h'. So it could never have 'dropped' them, since it never
had them.

-Mike


 
Reply With Quote
 
johanatan
Guest
Posts: n/a
 
      12-24-2007
On Dec 24, 11:24 am, "Mike Wahler" <(E-Mail Removed)> wrote:
> >> Note that <string.h> (and <cstring>) declare the 'C-style' string
> >> operations (e.g. strcpy(), and <string> declares the C++ std::string type
> >> and associated functions. Not the same thing at all.

>
> > I think this point could use a little expansion. Since the OP was
> > asking about <string.h> vs. <string>, it should be pointed out that
> > this is an STL-specific requirement and the info provided about
> > standard C libs in C++, though useful, is slighty OT.

>
> The standard C headers (both those with '.h' and those without) are
> defined by the standard, so they're certainly topical for comp.lang.c++.
>


I meant OT with respect to the poster's question. Obviously, since
the poster's compiler was suggesting a change from <string.h> to
<string>, and it presumably still compiled after the change, I think
it's safe to assume that the issue was STL (however 'standard' it
might be with respect to the C++ language).

> > Circa 1999, STL
> > dropped all the .h's from its include files

>
> The first version of the C++ standard was dated 1998.
> Note that the C++ standard does not define 'STL', but defines
> a 'standard library' which includes many constructs from a
> pre-standard entity commonly known as 'STL'. The C++ standard
> has never defined any C++ library headers whose names contained
> a '.h'. So it could never have 'dropped' them, since it never
> had them.
>


I realize that STL isn't part of the C++ standard, but it is the
rightful explanation of why the OP's compiler wanted him to change
<string.h> to <string>. My memory might be off, but I thought that
(at least in the MS world), the introdution of namespace 'std'
coincided with dropping .h from STL headers (this might not have been
an issue with other compilers as they may have already been using
namespace 'std' without the .h's).

I don't think the issue here is the C++ standard as much as it is the
'Standard' template library. STL certainly requires currently that
you don't append a .h (however silent the C++ standard is on the
issue) no matter which platform you are on.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWIG-PYTHON -> Problem in header file inclusion abarun22@gmail.com Python 0 10-20-2007 07:45 AM
SWIG-PYTHON -> Problem in header file inclusion abarun22@gmail.com Python 0 10-19-2007 02:51 PM
How to avoid multiple definition of a variable by multiple inclusion of a header file lars.uffmann@rwth-aachen.de C++ 11 08-05-2006 10:24 PM
tool to check multiple inclusion of header file techBoy C Programming 6 03-15-2006 10:49 AM
tool to check multiple inclusion of header file techBoy C++ 6 03-14-2006 12:29 PM



Advertisments