Search code examples
c++includec++builder-xe2

#include <file> #include <file.h> - what's the difference?


Possible Duplicate:
Difference between <string> and <string.h>?

My specific example uses following clause:

#include <string>

If I use following clause instead

#include <string.h>

compiler ends with error

[BCC32 Error] utils.cpp(173): E2316 'getline' is not a member of 'std'

Line 173 in utils.cpp file is as follows:

while(std::getline(in, line, '\n'))

I thought that there is no difference between these two clauses. Now I am confused. What files are in fact included by these two clauses? Lets say, my C++ Builder installation has program directory C:\Program Files\RAD Studio\9.0 and include files are located in subdirectory C:\Program Files\RAD Studio\9.0\include.


Solution

  • They are two different headers. The convention in the C standard library is to have the headers ending with .h, whereas in the C++ standard library the convention is to miss out the file extension altogether. Some more detail from wikipedia:

    Each header from the C Standard Library is included in the C++ Standard Library under a different name, generated by removing the .h, and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'. The only difference between these headers and the traditional C Standard Library headers is that where possible the functions should be placed into the std:: namespace (although few compilers actually do this). In ISO C, functions in the standard library are allowed to be implemented by macros, which is not allowed by ISO C++.

    Other libraries follow different conventions. Boost, for instance, chooses .hpp as their C++ header extension of choice.