#include <file> #include <file.h> - what's the difference? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
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.

#include <string>
This includes the C++ string header.
#include <string.h>
This includes the C string header, with all identifiers in the global namespace. (Deprecated.)
#include <cstring>
This includes the C string header, with all identifiers placed in the std:: namespace.
Edit: Rule of thumb - C++ headers never end on ".h". Prefix the traditional C header name with "c" and drop the ".h" to keep the global namespace clean. Use ".h" for your project's C headers only. Use ".hpp" for C++-only headers.

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.

By convention, C (Procedural) headers ends by '.h' "string.h", "stdio.h"... and C++ (Object oriented mostly) don't include any extension: "iostream", "string" ...
Not sure if all headers follow this convention, but I think that the standards ones all do.

Related

Why is there a ".h" extension in <bits/stdc++.h>?

(As far as I know) C++ doesn't accept the ".h" extension on header files (as it's usually not present in other include statements) so how does the include <bits/stdc++.h> work in C++ and why does this have the ".h" extension?
why there is “.h” extension in <bits/stdc++.h>
Because the developer - who created the file - chose that name.
It is conventional to name headers with the suffix .h and the developer presumably followed such convention.
(As far as I know)c++ doesn't accept the ".h" extension header files
Your knowledge is wrong. Any "extension", including no extension are accepted. There is no limitation to how a header file can be named.
You've tagged [c++-standard-library], so I'll add that C++ standard headers are all named without a suffix, except for those inherited from the C standard library which use the conventional .h suffix (and which do have non-suffixed aliases). bits/stdc++.h is not a standard header even though it may be part of a standard library implementation.
C++ does not use the .h extension for standard library headers named in #include statements.
But bits/stdc++.h is not a standard header, so the standard library naming conventions wouldn't apply to it. It should never be used anyway.
There is no mandatory mapping, IIRC, from the name used in the include statement, to the filename. You can certainly use .h extensions on your own headers if you want, although it may get confusing if you mix C and C++ in a project.
(As far as I know)c++ doesn't accept the ".h" extension header files
That is an incorrect statement.
You can use any filename in that statement.
After the preprocessor has inserted all the files and the result is valid C++ code, then there will not be any errors during compilation.
The case with standard headers is that the C++ standard library developers have created the standard functionality in files without an extension. So the filename is "vector", so #include <vector> is used.
If the developers add a file "vector.hpp", (which has a statement #include <vector> inside the body of the file) then it has to be used as #include <vector.hpp>.

What is loaded into standard namespace? [duplicate]

This question already has answers here:
cmath vs math.h (And similar c-prefixed vs .h extension headers)
(4 answers)
Closed 6 years ago.
I am currently building my first more complex C++ program that contains an own namespace. Of course, I use several imports. Ehat I found to be strange is that function included with eg.
#include <math.h>
can be accessed within the workspace
a = cos(b)
where the cos function is part of math.h. On the other hand functions included with eg.
#include <fstream>
must be accesed via
std::ifstream
or similar. I would like to understand this
C++ standard library includes most of the C library (there are some hazy details around optional parts of the C library).
Since C has no concept of namespacing, everything included from the C library will be in the global namespace. Since <math.h> is a C header, its functions are put into global namespace.
Everything included from the C++ standard library will be in the std:: namespace, like std::ifstream when you included <fstream> header.
Where it gets fun, are aliases. As an example, <math.h> can also be included as <cmath>. The idea behind this was that you select whether you want the C symbols in global namespace (including <math.h>) or in the std:: namespace (including <cmath>), but this didn't work out and generally if you include the C++ version of the header (ie <cmath>), you will get both.
Generally, if a header can either be included via <foo.h> or via <cfoo>, its a C header. C++ standard library headers do not have these aliases (except when you have to deal with non-standard things like iostream.h that apparently still exist on some platforms).

Compiling a .CPP using GCC

how do I compile a .c or .cpp file using GCC?
I need to include some standard libraries (fstream, string, iostream) - how do I do this?
For clarification, here:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"ocgenerator.h";
#include"structures.h";
#include"util.h";
#include"util2.h";
using namespace std;
(my .h files are in the same directory as the src file)
If I use the command:
gcc src.cpp -o src.o
I get a lot of errors: memcpy, atoi, atol, strncmp, etc, ... "are not declared in this scope". What should I add to the command?
edit: Or is it a scope thing, and do I have to add std:: to all those functions?
memcpy and strncmp are declared in <cstring>, atoi and atol in <cstdlib>. Just include these headers to bring in their declarations.
Side notes :
No semicolon after preprocessor directives, including #include "".
No using namespace std;, especially not in headers !
(Why is "using namespace std" considered bad practice?)
Since you're building a C++ project, don't forget to link with the standard library via -lstdc++, or use g++ which forwards it to the linker for you.
Note that with GCC you don't have to prefix standard C functions with std:: (as they are also declared in the global namespace), but you should anyway for your code to be standard-compliant.
The cplusplus site helps out. If you search for a specific function, on the top you will find the necessary references for the code to compile:
http://www.cplusplus.com/reference/cstdlib/atol/
You can even check in the URL. In this case, you need 'cstdlib'.
Regarding the compiler, there are several available, g++ is a good option.
I also suggest creating a makefile for automating the building process, because it becomes an headache when your codebase grows.
For any library function you use in your code, read the man page for that function to see which header declares it, and #include that header in any source file that uses the function.
For example, man memcpy shows the memcpy(3) man page (the (3) refers to section 3 of the manual; use man 3 memcpy to specify the section).
NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
...
memcpy is part of the C standard library, and is declared in <string.h>. For C++ you can use <string.h>, but it's probably better to use <cstring>, which puts the function name in the std namespace. In general, each standard C header <foo.h> is duplicated as a C++ header <cfoo>.
I get a lot of errors: memcpy, atoi, atol, strncmp, etc,
memcpy and strncmp are declared in <cstring>. atoi and atol are <cstdlib> (or in <string.h> and <stdlib.h> if you're programming in C rather than in C++).
And be sure to use the g++ command, not the gcc command, to compile C++ source. They both invoke the same compiler (gcc compiles C++ code if the source file name ends in .cpp), but the g++ command adds some options that are needed for C++.

Why it seems not necessary to include some STL headers

I think some headers need to be included to compile with gcc(4.9),
#include <algorithm> // for std::transform
#include <numeric> // for std::adjacent_difference
However, I found it's not necessary include them at all, I can still call
for example, the following functions
std::adjacent_difference (V1.begin(), V1.end(), V2.begin());
std::transform(V2.begin(), V2.end(), V3.begin(), V4.begin(), std::divides<double>());
Maybe I have misunderstood the mechanism of including header files...Any hint?
It is quite typical for a header to include other headers. The headers included by header x will be included in any file that includes x. That should be trivial to understand once you grasp what including a file in another means. In this case, one of the standard headers happened to be included in another.
Files included by headers can change between versions. If you don't include a required header, your program may break under another (version of) standard library even though it may appear to work in the current implementation. Of course, this applies to other libraries as well.
The C++ standard says the following in section 17.6.5.2 [res.on.headers]:
A C++ header may include other C++ headers.
For your question, this means a compiler can just act as if you included other C++ headers. In your example, the compiler is allowed to include the <numeric> header if you include <algorithm>, or vice versa.
But that's not the whole story. The standard also says:
A C++ header shown in its synopsis as including other C++ headers
shall provide the declarations and definitions that appear in the
synopses of those other headers.
And:
The C standard headers [...] shall include only their corresponding
C++ standard header [...]
(Note that I am quoting from the last free C++11 draft. The final version of the ISO standard is not free. See https://isocpp.org/std/the-standard.)
The <utility> header is an example of a C++ header which is guaranteed to include another. Its synopsis explitly includes <initializer_list>. Which means a conforming compiler must accept the following:
#include <utility>
// #include <initializer_list> // not needed
int main()
{
std::initializer_list<int> x = {};
}
For the C headers, in contrast, this means that the following must not compile:
#include <stdio.h>
int main()
{
std::cout << "\n"; // must not compile
}
What the standard says is surely confirmed by your implementation's documentation. For example, the documentation for Visual C++ 2013 says:
A C++ library header includes any other C++ library headers it needs
to define needed types. (Always include explicitly any C++ library
headers needed in a translation unit, however, lest you guess wrong
about its actual dependencies.) A Standard C header never includes
another standard header.
The advice given here is a good one; do not depend on automatic inclusion. Explicitly include everything you need.

Pre processor directive conio.h c++

As in C++ header files are used without .h extension like <iostream> instead of <iostream.h> but its not same in case of <conio.h>. Why we can't use <conio>
The C++ standard specifies which headers are part of the C++ standard library. In addition to C++-specific headers, it includes the headers specified by the C standard. You can use them with their C names (e.g., #include <stdio.h>), and they put their symbols into the global namespace. You can use them without the .h extension and a c on the front (e.g., #include <cstdio>), and they put their symbols into the namespace std.
But that's only for the headers from the C standard. conio.h is not part of the C standard, so the C++ standard doesn't say anything about it.
conio.h is a C header, thus (traditionally) C headers had the .h extension for the system headers. C++ standard headers are mainly without this .h extension. As you may know, many C headers (those from the standard library) have C++ counterparts (like in C++ is )
Because conio.h is a C header, not C++ specific.
conio isn't part of the c++ standard, so you can't count on the compiler to know what it is. :(
In fact, i think it's usually only supported under windows.