Header file search path under Linux

Source: Internet
Author: User

For software published in a compressed package, in its directory there is usually a configuration script configure, which determines the compilation parameters (such as header file location, connection library location, etc.), and then generates makefile to compile the program. You can go to the directory of the software and execute the "./configure--help" command to view the use Help.

a program can correctly compile, link, run needs to meet 3 conditions: When preprocessing can find the header file, the connection can find the library (static library or dynamic library), the runtime can find the library. The following describes the search path for the header file

The GCC search header file has three strategies:

1. The default search directory, which is the compiler's own preset directory. Because it is the default, will be searched, so always the last to search.

2. Manually specify the search directory. Can be set by the environment variable C_include_path before executing the configuration command configure, or it can be specified by the-I option when the configuration command configure is executed.

3. Manually specify a directory that is not searched. This is specified using the-nostdin option when the configuration command is executed.

-nostdinc Options

Make the compiler not in the system default header file directory inside the your change file, general and-I joint use, clearly define the location of the header file.

-nostdin C + + options

Specifies that the standard path specified by g++ is not searched, but is still searched in other paths. This option is used in the Genesis libg++ Library

How GCC specifies the header file

In a program, there are two common ways to include a header file:

#include #include "headerfile.h"

When # include compile command--->---> system preset Directory---> Compiler presets .

When # include "Headerfile.h", compile with " source file current directory ----> compile command to specify directory---> system preset Directory---> Compiler presets " The sequential search header file.

compile command to specify directory

The "specified" header file directory is the one that compiles the program using the " -i" option to specify the directory. As an example:

mkdir /* temp directory, test with */ mkdir /* temp directory, test with */ export C_include_path=/work/aaa/INCLUDEecho'Main () {}'  | arm-linux-gcc -i/work/bbb/include-e-V-

Get the following output, where you can see the path and order of precedence when you look for the header file:

...   " ..... "  <.....> search starts here:/work/bbb//work/aaa/include ...
System Presets Directory

The system preset header file directory is set by the environment variable C_include_path, and the value of this variable is set before the configuration command configure is executed.

Compiler presets Directory

The compiler preset directory is determined by the compiler itself and is determined by the program code, which is not required by the worker to set or specify.

You can summarize the search path and priority order of the files:

1. If the source file uses double quotation marks to contain the header file, first locate the header file in the source file's current directory.
2. If "-i/some/dir" is used at compile time, look in/some/dir.
3. If the environment variable C_include_path is set, it is found in the specified directory.
4. Finally find in the path of the compiler preset, this is no need to specify, compile time must be in the path to search for the desired header file.

Therefore, when compiling the program, if there is no header file error, you can either set C_include_path or set the "-I" line to the compiler to specify the header file directory, which can be set before the configuration command configure C_include_path or cflags, If you do not set Cflags, its default value is "-g-o2", for example:

Export c_include_path= "/SOME/DIR/1:/SOME/DIR/2"
Export CFLAGS = "-g-o2-i/some/dir" #如果设置了C_INCLUDE_PATH, you can not set the CFLAGS

./configure

There is a better way, when you know exactly which dynamic library to use, you can use the Pkg-config command to learn the parameters at compile time and the parameters to connect when using this library.

Take a look at the command first:

Export Pkg_config_path=/work/tools/gcc-3.4.5-glibc-2.3.6/arm-linux/lib/pkgconfig

Pkg-config--cflags Uuid-i/work/tools/gcc-3.4.5-glibc-2.3.6/arm-linux/include

. header file search path how GCC looks for the required header files at compile time:


The-i option specifies the directory--->GCC environment variable c_include_path,cplus_include_path,objc_include_path---> Find the compiler default directory

If there is a given prefix when the GCC is installed, then it is

/usr/include
Prefix/include
Prefix/xxx-xxx-xxx-gnulibc/include
Prefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.8.1/include

Search path for header files under Linux

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.