GCC header file path and dynamic link library path during compilation

Source: Internet
Author: User

When you use GCC to compile a connection to generate an executable file, you will often encounter issues such as undefined variables, links, or failure to find the corresponding dynamic library when running the executable file, this article first introduces the options related to the header file path during GCC compilation and the search path sequence, then discusses the search path sequence of the dynamic library when compiled into an executable file, and finally explains after the executable file is generated, the search path sequence of the dynamic library when the file is running. Finding out the three search paths can basically solve the problem of undefined variables or dynamic libraries not found in GCC.

Header file path compilation related options
GCC can use option-I (uppercase) to specify the header file search path, that is, the folder where the header file is located. The specified path can be an absolute or relative path. For example, if there are files hello. C and include/testhead. h In the current path, there are two methods to access this header file:
In hello. C, # include "include/testhead. H" directly specifies the path of the header file, and then you can directly compile the GCC hello. C without option-I.
Hello. in C # include "testhead. H ", use: gcc-I include hello. C or GCC-I. /include hello. c. Specify the path of the header file in the compilation option.
In addition, option-nostdinc makes the compiler no longer look for header files in the default header file directory. It is generally used in conjunction with-I to specify the position of the header file.

Two non-types of header files: # include <> and # include "" Search rules
1) when you use the <> included header file, the path after the-I option is searched first (that is, the-I option when GCC is used for compilation, note that it is in uppercase ), the following is the path to the standard system header file.
2) the header file contained with "" will first search for the current working directory, and then the search path is the same as the path searched by the header file contained in "<>.

Header file search sequence
The search sequence of header files. You can use option-V during compilation to view the compilation details. For example, the GCC-v-I./include hello. C section is as follows:


The header file search sequence does not Recursively search in the directory. The search sequence rules are as follows (<> type header file search rules, because the "" Type header file first searches for the current working directory, the following search path is the same as the path searched by the header file contained in the <> symbol ):
First, the path specified by the parameter-I (when multiple paths exist in the specified path, search in the order of the specified path ).
Then find the GCC environment variable c_include_path (this variable is used to search for the header file of the C language) and cplus_include_path (for the header file of C ++). The method for setting the environment variable is the same as usual, for example. bashrc or. profile, for example:

  export C_INCLUDE_PATH="$HOME/github"
Find the default directory
  /usr/local/include  libdir/gcc/target/version/include  /usr/target/include  /usr/include
For C ++ programs /.. in/include/C ++/version, find that the target refers to the standard name of GCC compilation configuration, and the version is the version in GCC. For example, the relevant path search information obtained by using gcc-V hello. cpp is:
  

  Dynamic library options
Assume that we have two source files, one of which is the dltest. C code:

  #include <stdio.h>  void test_dl()  {      printf("Test DL\n");                                                                                 }
Another source file code hello. C is as follows:
  void test_dl();  int main(void)  {  test_dl();  return 0;  }
Run the GCC-shared-FPIC-O libdltest. So dltest. C command to create the dynamic library file libdltest. So. Here option-shared tells the connector source code to generate a shared library instead of an executable file. Option-FPIC parameter Declaration the code segment of the Link Library can be shared, so that the generated Object Module adopts a floating (relocable) address, PIC indicates the position independent code ). Please note that the name of the shared library we compiled this time is libdltest. So, which is also a naming convention for the Linux Shared Library: The suffix uses so, and the name uses libxxxx format. After creating the shared library, you can use the command gcc-L./-ldltest hello. C to connect to the dynamic library to generate executable files. Option-l adds the path to the path list of the Search Library file. Option-L specifies the database with the specified name for search. Note that the name is dltest, And the link is libdltest. so. This is a common practice in Linux. When compiling executable files, the search path sequence of the dynamic library is as follows:
First, GCC will find the-L option;
Then find the GCC environment variable LIBRARY_PATH. You can set this environment variable in. profile, and you can use option-V to view the value of LIBRARY_PATH during GCC final compilation;
Find the default directory/lib:/usr/local/lib, which was written in the program at the time of compile GCC.
Note that the search order above will not be recursively searched in the directory. After an executable file is generated, the search path sequence of the dynamic library is as follows:
First, specify the dynamic library search path when compiling the target code, that is, use the option-wl, rpath to specify the search path of the dynamic library when the program is running, such as gcc-wl,-rpath, include-L. -ldltest hello. c. The path will be searched during file execution. /include;
The dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH;
The dynamic library search path specified in the configuration file/etc/lD. So. conf is to add the absolute path of the dynamic library to the configuration file, and then run the command ldconfig to make the configuration file take effect;
Default dynamic library search path/LIB;
The default dynamic library search path is/usr/lib.
Similarly, the search sequence above will not be recursively searched in the directory. A simple approach to using a dynamic library is to copy the generated so file to/usr/lib, so that whether the file can be generated or executed, you can find the required so file.

References

Https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
Http://www.network-theory.co.uk/docs/gccintro/gccintro_23.html
Https://gcc.gnu.org/onlinedocs/gcc-4.9.1/cpp/Search-Path.html#Search-Path
Https://erex.sinaapp.com /? P = 126

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.