开发者

various header files and their uses [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am searching for some information. I have seen in many programmes following files i开发者_StackOverflow社区ncluded

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

I want to know more what the above header files are used for i.e. in which conditions which header file is used. Any link which clearly mentions which header file serves what purpose that will be great.


sys/types.h: "data types"

sys/socket.h: "main sockets header"

sys/un.h: "definitions for UNIX domain sockets"

unistd.h: "standard symbolic constants and types"


Header files are used for declaring items that are defined in some existing library.

If you want to use socket(), you'll need to include sys/types.h and sys/socket.h. If you want to use atan(), you'll need to include math.h. If you want to use printf(), you'll need to include stdio.h.

Knowing which header file is needed for a function is given in its documentation (man printf on Unix/Linux).

Knowing which function can be used for solving a problem is given by experience, stackoverflow and Google.


If you want to know what's in a header file, try looking at it, seriously: most will start with some comment describing the content if it's not obvious anyway.

If you want to know what part of a header file is being used by the program that's including, try removing it and looking at the error messages. That may also sound glib, but sadly there's generally not a better way to find this out. But, it may be that on one platform some functionality requires including two headers, whereas on some other platform only one of them is required (perhaps because the second header is indirectly picked up by some earlier include anyway): if you test on the platform where one header is needed and decide to remove the second include, you may break the build on the other platform. So, when you find stuff that is needed, consult the man pages for the required headers - they may be specified by some Standard that both platforms honour.

If you want to know which header files to use yourself, then again - you have to look at the documentation for the functions you need to call. Even then, as a C++ programmer you should prefer the C++ versions of certain C headers, and standard documentation tools like man - given the C function name - won't tell you about the C++ headers. Have a read of e.g. http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch03s02.html - it's GCC documentation but describes C++ Standard requirements for these headers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜