Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do compilers know where to find #include <stdio.h>?

I am wondering how compilers on Mac OS X, Windows and Linux know where to find the C header files.

Specifically I am wondering how it knows where to find the #include with the <> brackets.

#include "/Users/Brock/Desktop/Myfile.h"    // absolute reference
#include <stdio.h>                         // system relative reference?

I assume there is a text file on the system that it consults. How does it know where to look for the headers? Is it possible to modify this file, if so where does this file reside on the operating system?

like image 335
Brock Woolf Avatar asked Dec 14 '09 07:12

Brock Woolf


People also ask

Where does compiler look for header files?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets.

Where is the Stdio h file located?

In Ubuntu, which your original question indicated you're using, the “root” stdio. h file is in /usr/include .

Where is printf located?

printf is part of standard library, which is called "standard" because it is linked by default by C compiler. Standard library is typically provided by operating system or compiler. On most linux systems, it is located in libc.so , whereas on MS Windows C Library is provided by Visual C runtime file msvcrt. dll .

What is C++ compiler?

A compiler is a special program that translates a programming language's source code into machine code, bytecode or another programming language. The source code is typically written in a high-level, human-readable language such as Java or C++.


1 Answers

When the compiler is built, it knows about a few standard locations to look for header file. Some of them are independent of where the compiler is installed (such as /usr/include, /usr/local/include, etc.) and some of the are based on where the compiler is installed (which for gcc, is controlled by the --prefix option when running configure).

Locations like /usr/include are well known and 'knowledge' of that location is built into gcc. Locations like /usr/local/include is not considered completely standard and can be set when gcc is built with the --with-local-prefix option of configure.

That said, you can add new directories for where to search for include files using the compiler -I command line option. When trying to include a file, it will look in the directories specified with the -I flag before the directories I talked about in the first paragraph.

like image 52
R Samuel Klatchko Avatar answered Sep 20 '22 17:09

R Samuel Klatchko