Linux application development (1): GCC compiler

Source: Internet
Author: User
Tags mathematical functions

1. Push:

1. GCC is a Linux-based compiler launched by GNU. GCC can compile C, C ++, and assembler programs,

2. in Linux, whether a file can be executed is determined by the file name suffix, but by the file attributes (-x;

3. GCC uses the file name suffix to differentiate the type of the input file. For example:


(1) c source code files that have been preprocessed by ". c", C language, and ". I"

(2). ". s", assembly language, and ". s" are pre-compiled assembly language source code files.


(3) ". C,. CC,. cxx", C ++, and ". II" Source code files that have been processed with C ++

(4). ". H", header file

(5). ". O": the compiled target file. ". A" is a library file consisting of the target file.

4. preprocessing is also called pre-compilation. Its main task is simply to replace text, specifically to process commands starting with #. (1) macro expansion (2) header file inclusion (3) Conditional compilation

Ii. Basic GCC usage (there are more than 100 GCC Command Options. Fortunately, we only use more than 10 commands. Here we only list the commonly used options in the development process ):

1. GCC command format:


GCC [Options] [filenames],


Options, compilation options,


Filename, the name of the file to be compiled,

(PS. [] indicates that the option does not exist)


2. Options:

(1)-O output_filename: Specifies the name of the executable file generated after compilation: output_filename. If not specified, the default value is a. Out.

(2)-C: tells the compiler to only complete compilation without linking to an executable file, as long as the target file is generated. o. This command is not commonly used in application development and is mainly used in Linux kernel compilation.

(3)-G: Tell the compiler to inject the symbolic information required by the debugging tool (gnu gdb) into the generated file. To debug the compiled program, you must add this command (of course, the file generated after this command is added will become larger ).

(4) optimization commands:-o,-O2, both of which are optimized for program compilation and links to improve the execution efficiency of executable files, of course, the corresponding Compilation Time will also increase.-O2 is only an advanced version of-o. (In a simple mathematical example, the execution time after optimization is reduced by 3 or 4 times)

(5)-idirname. Here the command is-I. dirname is connected with I, telling the compiler to add the specified directory dirname to the header file search list (PS. in the pre-processing phase, for the symbol <>, CPP (C pre processor Preprocessing Program) will be searched based on the default header file directory (/user/include, the function of this option is to tell CPP that if the header file cannot be found in the default directory, it will be searched here)

(6)-ldirname: add the directory specified by dirname to the directory list of the library file, and tell LD (Connection Program) to first find the library file in the directory specified by-L, then go to the system preset directory to find (/usr/lib)

(7)-lname: Specifies the function library required for Link. For example, when mathematical functions are used in the source code, we need to link the function library libm. M is short for a, so we can add the option-LM to link the math library function.

(8)-Staic, which is compiled using static links. Dynamic Links are usually suffixed with. So, and static links are suffixed with. O.

Note: here we will explain the problem of Dynamic Links and Dynamic Links: When static links are used, the linker will find out all the library functions and add their copies to the executable files (so the files generated by static links are very large). Once the static links are successful, this external function library is not needed. However, when dynamic links are used, the execution program only makes a mark in the program, indicating that when the program needs to be executed, the external function library must be loaded first, the advantage of this method is that it saves space. The default link of GCC in Linux is dynamic link.

(9)-wall, (w in upper case), print all warning information


-W, (lower case W), no warning information is printed

(10)-dmacro and-D can define a macro in the command. This option is equivalent to adding # define macro to the program.

 

 

 

 

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.