#include preprocessor instructions-file include

The #include preprocessor instruction is a preprocessor instruction to include other source code.

#include <filename>

#include "filename"

Embeds the contents of the file specified by the file name in the current source code.

The difference between "" and "" filename "" seems to depend on the preprocessor implementation.

This is the result of trying with gcc.

For "", the files are searched in the order of "directory specified by the -I option" and "system header file include path".

In the case of "" file name "", the file is searched by "current directory", "directory specified by -I option", and "include path of system header file".

If multiple "-I" options are specified, the I option specified earlier will be searched first.

When using standard library, or when using the library installed in the include path of the system header file, "", The header file for your own application should look like "" file name "".

#include Preprocessor instruction sample

#include Preprocessor instruction sample.

Read the standard library header

Load the standard library header.

#include <stdio.h>

int main (void) {
  printf("%s\n", "Hello World!");
}

Read the header file of your own application

Load the header file of your application.

#include "myapp_lib.h"

int main (void) {
  // Use functions declared in myapp_lib.h, etc.
}

Does the extension of the file to be read need not be ".h"?

Yes, the extension name doesn't matter. For example, ".c" can also be read. You can read it, but unless you really have a specific reason, you shouldn't read anything other than the header file.

Can the file name be an absolute path?

Yes, the file name can be an absolute path. However, unless you really have a specific reason, it's best to read with a relative path.

How can I find out the include path of the system header file?

"How should I find out!"

This is information statically written to the compiler's preprocessor, so you need to look up the preprocessor's information.

In the case of Ubuntu, there is an article that describes how to check it, so please refer to it.

Why can I use the standard library just by including the header file?

"Why can I use the standard library just by including the header file? Why can I use it without linking an object file like libstdio.o?"

As explained in the case of gcc, there is an object file called "libglibc.o (or libglibc.so)" that implements standard library such as "printf function". , Because it is implicitly linked.

Associated Information