The use of the GCC compiler in Linux

Source: Internet
Author: User
Tags mathematical functions

GCC is a very important compilation tool for development in a Linux environment, so it is necessary to learn the basic common usage of GCC.

  First, let's start by explaining the suffix name type of the source file under GCC compilation.

. c is a suffix file, C language source code file;
. A is a suffix file, which is the archive file consisting of the target file;
. c,.cc or. cxx as the suffix of the file, is a C + + source code file;
. h is a suffix file, which is the header file contained in the program;
. I is the suffix of the file, is already preprocessed C source code files;
. II is a suffix file, which is a pre-preprocessed C + + source code file;
. m is the suffix of the file, is objective-c source code file;
. O is the suffix of the file, is the compiled target file;
. s is the suffix of the file, is the assembly language source code files;
. S is the suffix of the file, is a precompiled assembly language source code files.

  

  Two. Next, we will describe specifically the use of the GCC command.

The basic command format for GCC is: gcc [options] [filenames]

Where [options] is the compiler needs to make the appropriate options, filenames is the name of the relevant file, below I will introduce the next few options

( Note: Be sure to distinguish between the case in the compilation options, they represent different meanings )

(1)-C: Just compile without connecting to the executable file, the compiler is only generated by the input. C source code files. O is the suffix of the target file, typically used to compile a subroutine file that does not contain the main program.

(2)-O Output_filename: Determines the output file name is Output_filename, if not given this option, GCC gives the default executable file a.out.

(3)-G: the symbolic information necessary to produce the symbolic debugging tool (GNU GDB), to debug the source code, we must add this option.

(4)-O: Optimize the program to compile, connect, with this option, the entire source code will be in the compilation, connection process optimization processing, so that the resulting executable file execution efficiency can be improved, but the speed of the compilation, connection will be slower.

(5)-o2: Better than-O to optimize the compilation, connection, of course, the entire compilation, the connection process will be slower.

(6)-idirname: adds the directory indicated by DirName to the program header file directory list , which is the parameter used during the precompilation process.

The header file in the C program contains two scenarios:

A. #include <myinc.h>

B. #include "myinc.h"

Where Class A uses <> to contain the header file, Class B uses "" To include the header file. The main difference between them is that for Class A, the preprocessor source file searches for the corresponding file in the system preset including the file directory (such as/usr/include, etc.), and for Class B, the source file searches for the header file in the current directory , which is the function of telling the source file, If the desired header file is not found in the current directory, go to the specified dirname directory to find it. In the program design, if we need this kind of inclusion file to be distributed in different directories, we need to use the-I option to give the search path individually.

(7)-ldirname: adds the directory indicated by dirname to the directory list of the program function link library file , which is the parameter used during the connection process. In a preset state, the connector LD looks for the required link library files in the system's default path (such as/usr/lib). and this option tells the connection program, first to the-l specified directory to look for, and then to the system preset path to find, if the function inventory placed in multiple directories, you need to use this option in turn, give the corresponding storage directory.

(8)-lname: When the program is connected, it loads a function library named "Libname.a" , which is located in the System preset directory or in the directory determined by the-l option. For example,-LM represents a library of mathematical functions named "LIBM.A",

"-lpthread" means the link library file for the link thread function.

(9)-shared: This is primarily used when generating shared library files.

()-wall: Generates all warning messages.

(one)-W: No warning message is generated.

()-S: Generates assembly language files.

  Three. Finally, I would like to explain to you in the ordinary people do not pay much attention to a problem-------GCC is how to find the header files and links in the source files required by the link library file

(1) Find header file

Step1: Search in the same directory as the source file

Step2: When we compile the program with the option "-idirname" refers to the path to find the header file.

Step3: Find the path set by the GCC-related environment variable: c_include_path,cplus_include_path,objc_include_path

Step4: Find the GCC default directory:

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

(2) Find a dynamic link library

What is the link library? Let me say one more word. As an example of the printf function commonly used in our C language, do you know how the printf function is implemented? In fact, it is the system to the implementation of these functions are known as the libc.so.6 library file, when not specifically specified, Gcc will go to the system default search path "/usr/lib" to find, that is, linked to Libc.so.6 the library function so that it can implement the function "printf", which is the function of the link. The function library is divided into two kinds: static function library and dynamic function library. Static library refers to the compilation of links, the library files of the code are all added to the executable file, so the resulting file is relatively large, but at run time also no longer need the library file. The suffix is generally ". a". Dynamic libraries In contrast, when compiling a link does not add the code of the library file to the executable file, but the library is loaded by the runtime link file when the program executes , which can save the system overhead. The general suffix of the dynamic library is named ". So", as described in the previous libc.so.6 is a dynamic library.

Well, to the end, the dynamic link library is actually looking for the same as the lookup header file.

Step1: Find in the directory specified by the option "-ldirname".

STEP2:GCC the environment variable library_path the path specified in the search.

Step3: Found in the GCC-default directory, such as/usr/lib//usr/local/lib/

Related Article

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.