Linux C Language header file search path

Source: Internet
Author: User

This article describes the search path to the header file in Linux, which means that you can find it by using the header file specified by include, the GCC compiler under Linux. Before you do this, you should understand a basic concept.

The header file is a text file that is saved with the extension. h after the code is written using a text editor. In a header file, you typically put some code that is reused, such as function declarations, variable declarations, constant definitions, macro definitions, and so on. When a header file is referenced by using the # include statement, it is equivalent to copying all the contents of the header file to # include. #include有两种写法形式, respectively:

#include <>: Go directly to some of the directories specified in the system to find some header files.

#include "": Go to the folder where the source files are located, and then go to some of the directories specified in the system to find some header files.

#include文件可能会带来一个问题就是重复应用, such as a function referenced by A.H is an implementation, and b.h refers to this function is another implementation, so at compile time will be error. Therefore, in order to avoid compilation errors caused by repeated references, header files often have:

#ifndef LABEL

#define LABEL

Code section

#endif

The format. Where label is a unique label, the naming convention is the same as the naming convention for variables. It is often named according to its header file name, for example, if the filename of the header is called hardware.h, then this can be used:

#ifndef __hardware_h__

#define __hardware_h__

Code section

#endif

This means that if you do not define __hardware_h__, define __HARDWARE_H__ and compile the following code section until you encounter #endif. In this way, when a duplicate reference is made, the following code section is not compiled because __HARDWARE_H__ is already defined, thus avoiding duplicate definitions.

In a word, the header file is actually just a few of the common commands in the integration, you need to use which command to load which header file on it.

GCC look for path to header file (in order of 1->2->3)

1. When GCC compiles the source file, the search path of the header file is specified by the parameter-I, and if there are multiple paths to the specified path, the header file is searched in the order specified by the path. command form such as: "Gcc-i/path/where/theheadfile/in sourcefile.c", where the path of the source file can be either an absolute path or a relative path. eg

Set the current path to/root/test,include_test.c if you want to include the header file "Include/include_test.h", there are two methods:

1) include_test.c include "Include/include_test.h" or # include "/root/test/include/include_test.h", then GCC Include_ TEST.C can

2) INCLUDE_TEST.C # include <include_test.h> or # include <include_test.h>, and then gcc–i include include_test.c can also

2. Search for the header file location by finding the environment variable C_include_path/cplus_include_path/objc_include_path for GCC.

3. Find the default directory search, respectively

/usr/include

/usr/local/include

/usr/lib/gcc-lib/i386-linux/2.95.2/include

The last line is the library file address of the GCC program, which may not be the same on each user's system.

By default, GCC is assigned to the/usr/include folder to find the header file.

GCC also has a parameter:-nostdinc, which makes the compiler no longer the system default header file directory inside the your change file, general and-I union use, clearly qualified header file location. When compiling the driver module, because of the extraordinary demand must force GCC does not search the system default path, that is, do not search/usr/include to use the parameter-nostdinc, but also use the-i parameter to specify the kernel header file path, this time must be specified in the makefile.

4. When # include uses a relative path, GCC eventually builds the position of the head file based on these paths. If # include <sys/types.h> is a file/usr/include/sys/types.h

Https://www.cnblogs.com/myitm/archive/2012/12/25/2832347.html

Http://bbs.chinaunix.net/thread-4097853-2-1.html

Linux C Language header file search path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.