GCC compilation option research (2)

Source: Internet
Author: User
Tags switch case

VII. Preprocessing options

(1)-include file: Before processing a regular input file, the file is compiled.

For example:

Ctest. c <br/> ----------------------------- <br/> # include <iostream> <br/> using namespace STD; <br/> int main () {<br/> int I = 0, j = 0; <br/> int A [5] = {1, 2, 3, 4, 5 }; <br/> int * P; <br/> int * q = a + 5; <br/> for (P = A; P <q; P ++) {<br/> cout <* P <Endl; <br/>}< br/> cout <"The End" <Endl; <br/> comp (11, 12); <br/>}< br/> ----------------------------------- <br/> ctest. CC <br/> --------------------------------- <br/> # include <iostream> <br/> using namespace STD; <br/> int comp (int A, int B) {<br/> cout <A <B <Endl; <br/>}

A. When g ++ ctest. C is used, an error is returned:

Ctest. C: In function 'int main ()':
Ctest. C: 13: Error: 'comp 'has not been declared in this scope

B. Use G ++-include ctest. CC ctest. C to compile and run

C. Add # include "ctest. cc" to ctest. C"
:

D. Use G ++ ctest. C to compile and run the program successfully. Use G ++-include ctest. CC ctest. C to report "Duplicate function Declaration"

(2)-before processing regular input files, imacros file first processes include file, but ignores the output results, so that only the macro of file is effective.

For example:

Ctest. c <br/> ------------------------------ <br/> # include <iostream> <br/> using namespace STD; <br/> int main () {<br/> int I = 0, j = 0; <br/> int A [5] = {1, 2, 3, 4, 5 }; <br/> int * P; <br/> int * q = a + 5; <br/> for (P = A; P <q; P ++) {<br/> cout <* P <Endl; <br/>}< br/> cout <"The End" <Endl; <br/> comp (11, 12); <br/> int c = max (12, 13); <br/> cout <C <Endl; <br/>}< br/> --------- ------------------------ <Br/> ctest. CC <br/> ---------------------------------- <br/> # define max (A, B) (A> B? A: B) <br/> int comp (int A, int B) {<br/> return a; <br/>}

A. When g ++ ctest. C is used, the following error occurs: comp and Max are not declared.

B. Use G ++-include ctest. CC ctest. C to compile and run

C. use G ++-imacros ctest. CC ctest. C will report an error: the comp is not declared, so it can be seen that-imacros only makes the macro effective, and ignores the compiled file ctest. CC output result

 

(3)-idirafter dir

(4)-iprefix prefix as the prefix of the-iwithprefix Option

(5)-iwithprefix dir: add the directory to the second include path. The directory name is composed of prefix and Dir, And the prefix is-iprefix.

For example, if I have a head file in/root/cpptest/INC named ctest. H, in my ctest. C # include "ctest. H"

A. G ++ ctest. C will report an error saying that the ctest. h file cannot be found.

B. G ++-idirafter/root/cpptest/INC/ctest. C will be compiled and run

C. The same effect as B G ++-iprefix/root/cpptest/-iwithprefix Inc ctest. CC

 

(6)-nostdinc does not search for header files in the standard system directory. It only searches for files in the directory specified by-I.

(7)-nostdinc ++ does not search for header files in the C ++ standard system directory, but still searches for files in other Standard Directories.

For example:

# Include <stdio. h> <br/> int main () {<br/> int I = 0, j = 0; <br/> int A [5] = {1, 2, 3, 4, 5 }; <br/> int * P; <br/> int * q = a + 5; <br/> for (P =; P <q; P ++) {<br/> printf ("% d/N", * P); <br/>}< br/>}

A. If gcc-I.-nostdinc ctest. c fails, "stdio. h file not found" is reported.

B. GCC-I. ctest. C is successful and can be run.

C. g ++-I.-nostdinc ++ ctest. C is successfully run.

D. in ctest. add the # include <iostream> and using namespace STD; rows in C, and then G ++-I. -nostdinc ++ ctest. c. "The iostream file is not found"

 

(8)-C tells the Preprocessor not to discard comments and use them with-e.

Gcc-e-c ctest. c

(9)-P tells the pre-processor not to generate # Line command, used with-e

 

(10)-M tells the Preprocessor to output a suitable make rule.

(11)-Mm outputs a make rule, involving only user header files

(12)-MD outputs a make rule and outputs it to The. d file.

(13)-MMD outputs a make rule, which only involves the user header file and is output to the. d file.

The output of GCC-M ctest. C is similar to the following:

Ctest. O: ctest. c/usr/include/stdio. h/usr/include/features. h/
/Usr/include/sys/cdefs. h/usr/include/bits/wordsize. h/
/Usr/include/GNU/stubs. h/usr/include/GNU/stubs-32.h/
/Usr/lib/GCC/i386-redhat-linux/4.3.2/include/stddef. h/
/Usr/include/bits/types. h/usr/include/bits/typesizes. h/
/Usr/include/libio. h/usr/include/_ g_config.h/usr/include/wchar. h/
/Usr/lib/GCC/i386-redhat-linux/4.3.2/include/stdarg. h/
/Usr/include/bits/stdio_lim.h/usr/include/bits/sys_errlist.h

 

(14)-H output referenced header files

Gcc-H ctest. C, output:

./Usr/include/stdio. h
../Usr/include/features. h
.../Usr/include/sys/cdefs. h
.../Usr/include/bits/wordsize. h
.../Usr/include/GNU/stubs. h
.../Usr/include/bits/wordsize. h
.../Usr/include/GNU/stubs-32.h
../Usr/lib/GCC/i386-redhat-linux/4.3.2/include/stddef. h
../Usr/include/bits/types. h
.../Usr/include/bits/wordsize. h
.../Usr/include/bits/typesizes. h
../Usr/include/libio. h
.../Usr/include/_ g_config.h
.../Usr/lib/GCC/i386-redhat-linux/4.3.2/include/stddef. h
.../Usr/include/wchar. h
.../Usr/lib/GCC/i386-redhat-linux/4.3.2/include/stdarg. h
../Usr/include/bits/stdio_lim.h
../Usr/include/bits/sys_errlist.h
Multiple anti-re-inclusion methods may be useful to them:
/Usr/include/bits/stdio_lim.h
/Usr/include/bits/sys_errlist.h
/Usr/include/bits/typesizes. h
/Usr/include/GNU/stubs-32.h
/Usr/include/GNU/stubs. h

 

(15)-D definition macro

Gcc-DPI = 3.14 ctest. c

(16)-u cancel macro

(17)-DM outputs a valid macro definition list (used with-E)

Gcc-e-DM ctest. c

 

For other options, see the GCC documentation.

 

8. Assembly options

-Wa, option ---- Not familiar


9. connector options (used to connect to the target file)

(1)-llibrary connects to the library file named liblibrary. A (search for the standard directory and the directory specified by-l)

Gcc-c-o libctest. A ctest. CC generates library files

Gcc-L.-lctest ctest. c search for the libctest. A library file in the current directory

(2)-lobjc connects to the Objective C program

(3)-nostartfiles does not connect to the standard Startup File

(4)-nostdlib does not connect to standard startup files and standard library files

(5)-static supports dynamic connection to the system to prevent connection to the shared library

(6)-shared to generate a shared library (. So)

Gcc-shared ctest. CC-O ctest. So

For other options, see the GCC documentation.

 

10. DIRECTORY Options

(1)-Idir header file search directory

Gcc-I./INC/ctest. c

(2)-I-any path specified before-I is only used for search # include "file", not for search # include <File>

(3)-ldir-l search the path of the Library File

Gcc-L.-lctest ctest. c search for the libctest. A library file in the current directory

(4)-bprefix: Where to find executable files, library files, and compiler data files

G ++-B.-lctest ctest. c SEARCH FOR THE libctest. A library file in the current directory

 

11. Warning options

(1)-W disables all warning information

Gcc-W ctest. c

(2)-W warning

-Wimplicit-in warning no declaration of the specified type

-Wimplicit-function-declaratiion warning to use the function before Declaration

-Wimplicit is equivalent to the two above

-Wmain: warning when the main is declared as a strange type

-Wreturn-type should return values to the function, but no warning is returned.

-Wunused: The local variable is declared but not used.

-Wswitch indicates that no value in the index structure is used in the switch case. If the input value is an index variable

-Wcomment comments/* In comments

-Wformat: Make sure that the formats of functions such as printf are consistent with those of actual parameters.

-Wuninitialized: Use the variable (used with-O) before initialization)

-Wparentheses ignores some parentheses.

-Wall

Gcc-wall ctest. c

The following is not included in-wall:

-Wshadow: one local variable shields the other.

-Werror warning is regarded as an error

Gcc-werror ctest. c

There are also many other warning options, see the GCC Manual

For example, this Code contains many warnings in-wall.

# Include <stdio. h> <br/> # define PI 3.14 <br/> # line 100 <br/> double main () {<br/> int I = 0, j = 0; <br/> int A [5] = {1, 2, 3, 4, 5}; <br/> int * P; <br/> int * q = a + 5; <br/> for (P = A; P <q; P ++) {<br/> printf ("% d/N", * P ); <br/>}< br/> // comment <br/> printf ("% F/N", Pi ); <br/> printf ("% d/N" ,__ line _); <br/> short K = 123456789; <br/> comp (12, 13 ); <br/> Enum myenu {A, AA, AAA}; <br/> Enum myenu myinput = A; <br/> switch (myinput) {<br/> case 4: <br/> printf ("% s/n", "A"); <br/> break; <br/> case AA: <br/> printf ("% s/n", "AA"); <br/> break; <br/> case AAA: <br/> printf ("% s/n", "AAA"); <br/> break; <br/>}< br/>/* test inner */<br/> printf ("% d/N", Pi ); <br/> int U; <br/> J = u + 1; <br/> {<br/> int I = 100; <br/> I ++; <br/>}< br/> int comp (int A, int B) {<br/>}< br/>

 

12. debugging options

(1)-G generates debugging information in local format. GDB can use the debugging information.

(2)-generate debugging information in the local ggdb format, including GDB extension as much as possible

(3)-glevel

-Ggdblevel

Level = 1, 2, 3 1 -- Minimum Information 2 -- default, 3 -- maximum

(4)-P generates additional code for profile for Prof

(5)-PG generates additional code for profile for GPROF

(6)-save-temps to save the temporary file

There are many other debugging options, see the GCC Manual

 

13. Optimization Options

(1)-O Optimization

-O1 General Optimization

-O2 is more optimized than-O1.

-O3 is more optimized than-O2.

-O0 not optimized

There are many other optimization options, see the GCC Manual

 

14. Target machine options

By default, GCC compiles the local target code, and can also compile the code on other machines.

(1)-B machine target machine

(2) which GCC version is used for-V version?

 

15. Machine related options

Special options for each target model are guided by the "-M" switch. For details, see the GCC manual.

 

16. Code Generation Options

It is used to control the generation of the target code and has nothing to do with the platform. For details, see the GCC manual.

 

17th. Pragma

Gnu c ++ supports two '# pragm' commands to enable the same header file to have two purposes: Interface Definition of the object class and complete content definition of the object class.
# Pragma Interface

(Only for C ++) in the header file that defines the object class, using this command can save the size of most target files using this class. generally, the local sub-parts of some information (embedded member function backup sub-parts, debugging information, internal tables for implementing virtual functions, etc.) must be stored in each target file containing the class definition. this pragma command can avoid such replication. this auxiliary information is not generated when the header file containing the '# pragma interface' command is referenced in the compilation (unless the' # pragma implementation 'command is used in the input main file ). as an alternative, the target file will contain references that can be parsed during connection ).

# Pragma implementation
# Pragma implementation "objects. H"


(Only for C ++) If you want to generate a complete output (globally visible) from the original file, you should use this Pragma in the main input file. the '# pragma interface' command should be used in the header file in sequence. the implementation file will generate backup and debugging information for all embedded member functions, and implement internal tables of virtual functions. if '# pragma implementation' does not contain a parameter, it refers to the inclusion file with the same basic name as the source file; for example, 'allclass. in CC, '# pragma implementation' is equal to '# pragma implementation allclass. H '. if an implementation file needs to introduce code from a multi-head file, use this string parameter. it is impossible to divide the content in a header file into multiple implementation files.

 

18. Files And Directories

File. C source file
File. h c header file (preprocessing file)
File. I: pre-processed C source file
File. C ++ source file

File. cc c ++ source file
File. cxx C ++ source file
File. m objective-C source file
File. s assembly language file
File. O target file
Output file of A. Out connection

Standard Directory of/usr/include # include file

/Usr/include/<library>/header file of the Library

/Lib Library File

/Usr/lib Library File

/Usr/lib/<library>/library file of a component

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.