GCC compiler (2)

Source: Internet
Author: User
ArticleDirectory
    • 4. Warning options
    • 5. connection options

GCC compiler (1)

4. Warning options

During compilation, the compiler reports errors and warningsProgramThe GCC contains the complete error check and warning function, which can help Linux programmers identify the error or potential error as soon as possible.CodeTo write better code. The following table lists the GCC compiler warning options:

Type Description
-Wall Enable all warning information
-Werror Cancel the compilation operation when a warning occurs. The warning is regarded as an error.
-W Disable all warning messages

 

Let's take a look at a piece of code, compile it using gcc, and enable warning information at the same time:

 
# Include <stdio. h> void main () {int X; For (x = 1; x <= 10; X ++) {printf ("% d \ n", x );}}

The above code is compiled and connected:

$ Gcc-wall example3.c-O example3

Example3.c: 2: 6: Warning: The return type of 'main' is not 'int' [-wmain]

As shown in the preceding output, GCC provides a warning message, indicating that the return value of the main function is declared as void, but it should actually be Int.

In addition, GCC provides many options starting with-W, allowing users to specify a specific warning, such:

    • -Wcomment: a warning is issued when comments are nested.
    • -Wconversion: if implicit type conversion exists in the program, a warning is issued.
    • -Wformat: checks the format string and parameter type Matching of formatting input and output functions, such as printf and scanf. If no match is found, a warning is issued.
    • -Winline: If the function cannot be inline, a warning is issued.
    • -Wlong-long: if the long data type is used, a warning is issued.
    • -Wmain: If the return type of the main function is not int type or the number of parameters used to call the main function is incorrect, a warning is issued.
    • -Wmissing-declarations: if a global function is defined but not declared in the header file, a warning is issued.
    • -Wparentheses: in some cases, if parentheses are ignored, a warning is issued.
    • -Wreturn-type: If the function defines the return type and the default type is int, the compiler will issue a warning.
    • -Wuninitialized: If the automatic variables used are not initialized, a warning is issued.
    • -Wundef: if an undefined variable is used in the # If macro for judgment, a warning is issued.
    • -Wunused: If declared variables or static functions are not used, a warning is issued.

The following uses GCC to compile a program to demonstrate the necessity of enabling warning information:

 
# Include <stdio> int main () {Double X; printf ("% d \ n", X ); /* Here % F is mistakenly entered as % d */return 0 ;}

Compile the above program:

$ GCC example4.c-O example4

As you can see, there is no error in compiling. Run the executable file and the output result is:

$./Example4

134513689

This is not the expected output result. If the-wformat or-wall option is added to the above compilation, that is:

$ Gcc-wformat example4.c-O example4

Or

$ Gcc-wall example4.c-O example4

GCC provides the following warning information:

Example4.c: In the 'main' function:

Example4.c: 5: 5: Warning: Format '% d' expects argument of Type 'int', but Argument 2 has type 'double' [-wformat]

If the format string does not match the parameter type, the program runs incorrectly. Therefore, this is a useful warning option.

Next we will use GCC to compile a program and use the-wparentheses option to check the brackets.

 
# Include <stdio> int main () {int A = 1; int B = 0; int c = 1; if (A & B | C ){;} if (A = 1) if (B = 1) printf ("B = 1 \ n"); elseprintf ("B! = 1 \ n "); Return 0 ;}

 

Compile the above program:

$ Gcc-wparentheses example5.c-O example5

Example5.c: In the 'main' function:

Example5.c: 7: 5: warning: we recommend that you add brackets [-wparentheses] to '&' in '|'.

Example5.c: 11: 7: warning we recommend that you explicitly use curly braces to avoid ambiguous 'else' [-wparentheses]

Therefore, the warning options of the GCC compiler are very important for programmers.

 

5. connection options

The following table lists the connector options provided by the GCC compiler:

Type Description
-Idirectory Add a new directory to the GCC header file search path
-Ldirectory Add a new directory to the GCC library file search path
-Llibrary Prompt that the Connection Program contains the specified library file when creating an executable file
-Static Force static Link Library
-Shared Generate dynamic library files

 

First, let's take a look at the two concepts of header files and library files:

The header file contains the declaration of variables and functions, but does not define the implementation of functions. The specific implementation of functions is completed in real library files. library files can be divided into static libraries and dynamic libraries, add all the code of the library file to the executable file, so that the library file is not required during running. The suffix of the static library is generally ". ". A dynamic library does not add the code of the library file to the executable file during compilation and connection, but connects the file to load the library file during program execution, this saves the system overhead. The suffix of the dynamic library is generally ". So ".

For example, we use the-I option to specify the path of the header file:

$ GCC example. C-o example-I/home/XXX/include

If the library file corresponding to the header file is not specified, GCC searches for it in the default search path.

Use the-L option to specify the path of the library file, for example:

$ GCC example. C-o example-L/home/XXX/lib

The GCC compiler uses a dynamic library by default, but if the-static option is used, the connector ignores the dynamic library and forces the static link library to use the following command:

$ GCC example. C-o example-static-LM

In this case, all the code in the static library file is included in the executable file, so the generated executable file is large.

 

 

After this section...

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.