开发者

C++ includes with and without .h [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What is the difference between using #include<filename> and #include<filename.h> in c++

I've never noticed it making any difference whether or not I include the .h at the end of an include, so I've always ignored the meaning, but I've just noticed in a particular program of mine, I get the error "memcpy was not declared in this scope" if I include "string", but not if I include "string.h".

First of all, I was wondering t开发者_JAVA技巧he specific cause of this, but also generally the difference between the two. At the same time, if someone could explain the difference between includes in angular brackets and those in quotation marks, It'd be much appreciated.


<string> is the C++ standard library string header file containing std::string and its friends. <string.h> is a different header, from the C standard library, which has functions for manipulating C strings (null-terminated strings) and other related functions.

The two are entirely different and unrelated. In C++ (as in C), a header file can have any extension. The C++ standard library headers have no extension; the C standard library headers have a .h extension. .hpp or .hxx are also common.


I'll just add, that in C++ there are C headers available in a traditional C form like string.h and those are used like #include <string.h>, but there are also their counterparts with the names starting with the letter "c" and without an extension - like cstring. Those headers are used like C++ headers #include <cstring> and the names from those headers are in the std namespace.


They are 2 different files and have nothing to do with each other.

#include <string> refers to the C++ standard library STL strings

#include <string.h> defines several functions to manipulate C strings and arrays.

No extension is assumed for includes.

Some header files don't have .h appended to them. You commonly see this with header files that contain templates.


About the difference between includes in angular brackets and those in quotation marks, the first instructs the preprocessor to search for include files first along the path specified by the /I compiler option. The second tells the preprocessor to look for include files in the same directory of the file that contains the #include statement.


Concerning the difference between using angular brackets and quotation marks, i think it has something to do with the order in which the header files are looked for : if it's between quotation marks, the compiler will first check the working directory (not very useful in the case of system headers, since they're not in the same directory as the compiler) before checking the system headers directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜