Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are header files organized in linux

Tags:

c

linux

On linux, there is socket.h under many folders as shown below.

  1. How is it decided as to what is stored under asm folder vs linux vs bits. I am assuming everything under sys is simply libc

  2. In what order do the header files get resolved by gcc. Does it for example pick socket.h from sys, then linux, then sys, then bits so on?

/usr/include/sys/socket.h, (used by libc)

/usr/include/linux/socket.h

/usr/include/asm/socket.h

/usr/include/asm-generic/socket.h

/usr/include/bits/socket.h (included by libc)

like image 869
Jimm Avatar asked Oct 23 '25 16:10

Jimm


1 Answers

1) A bare #include <socket.h> won't work in default linux configurations:

fatal error: socket.h: No such file or directory

This means that you have to specify the subdirectory of a directory in the gcc search path also: #include <sys/socket.h> (relative to /usr/include, so the absolute path is /usr/include/sys/socket.h). To determine what header file to use, look at man socket.

In general /usr/include/sys contains end-user headers, whereas bits/asm contain intermediate headers, used by gcc and other headers. /usr/include/linux contains Linux-specific headers (often shared with the kernel, so it's description of kernel structures that may be useful for userspace programming).

Libc headers are generally scattered throughout the whole /usr/include. Gcc-specific headers are often in places like /usr/include/x86_64-linux-gnu and such.

2) Here: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html you can find more information about the GCC search paths. None of the guessed directories are searched, just /usr/include and others on the list in the link.

like image 159
Dmytro Sirenko Avatar answered Oct 26 '25 06:10

Dmytro Sirenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!