C compiler is one of the indispensable parts to parse C program. For Linux system, GCC C compiler is undoubtedly one of the most popular C compilers. For GCC C compiler, many friends feedback that its operation is difficult. In order to help you use GCC C compiler more smoothly, this paper explains the main parameters of GCC C compiler.

How to compile files into assembly code with compiler?

GCC and G + + are GNU’s C & C + + compilers respectively. When GCC / G + + compiles, there are four steps

1. Preprocessing, generating. I file [preprocessor CPP]

2. Convert the preprocessed file into assembly language to generate file. S [compiler EGCS]

3. There is a file [assembler as] that assembles into object code (machine code) to generate. O

4. Connect object code to generate executable program [linker LD]

-x language filename

Set the language of the file to make the suffix invalid, which is valid for later multiple files. That is, according to the Convention, the suffix name of C language is. C, while the suffix name of C + + is. C or. CPP. If you are very personalized and decide that the suffix name of your C code file is. Pig, ha ha ha, then you need to use this parameter. This parameter will work for all the following file names, unless the next parameter is used. Can I use the following parameters: C, Objective-C, c-header, C + +, CPP output, assembler, and assembler with CPP.

It should be understandable to see English.

Example usage:

gcc -x c hello.pig

-x none filename

Turn off the previous option, that is, let GCC automatically identify the file type according to the file name suffix.

Example usage:

gcc -x c hello.pig -x none hello2.c

-c

It only activates preprocessing, compilation, and assembly, that is, it only makes programs obj files

Example usage:

gcc -c hello.c

He will generate the obj file of. O

-S

To only activate preprocessing and compilation means to compile a file into assembly code.

Example usage:

gcc -S hello.c

It will generate. S assembly code, which you can view with a text editor.

-E

Only activate preprocessing. This does not generate a file. You need to redirect it to an output file.

Example usage:

gcc -E hello.c 》 pianoapan.txt

gcc -E hello.c | more

Take your time. A hello word has to be processed into 800 lines of code.

-o

Set the target name. By default, the file compiled by GCC is a.out, which is hard to hear. If you and I have the same feeling, change it, ha ha.

Example usage:

gcc -o hello.exe Hello. C

gcc -o hello.asm -S hello.c

-pipe

Using pipeline instead of temporary files in compilation may cause some problems when using non GNU assembly tools.

gcc -pipe -o hello.exe hello.c

-ansi

Turn off the features in GNU C that are not compatible with ANSI C, and activate the proprietary features of ANSI C (including banning some ASM inline type of keywords, UNIX, VAX and other preprocessing macros).

-fno-asm

This option implements part of the function of the ANSI option, which prohibits the use of ASM, inline, and typeof as keywords.

-fno-strict-prototype

It only works for G + +. With this option, G + + considers functions without parameters to have no explicit description of the number and type of parameters, rather than no parameters.

No matter whether GCC uses this parameter or not, it will consider that there is no explicit type for functions without parameters.

-fthis-is-varialble

That is, to keep up with the traditional C + +, you can use this as a general variable.

-fcond-mismatch

It is allowed that the second and third parameter types of the conditional expression do not match, and the value of the expression will be void.

-funsigned-char 、-fno-signed-char、-fsigned-char 、-fno-unsigned-char

These four parameters are used to set the char type. It is decided to set the char type to either unsigned char (the first two parameters) or signed char (the last two parameters).

-include file

To include a certain code, simply put, is to use it to set a file when another file is needed. The function is equivalent to using # include in the code.

Example usage:

gcc hello.c -include /root/pianopan.h

-imacros file

The macro of file file is extended to the input file of GCC / G + +, and the macro definition itself does not appear in the input file.

-Dmacro

It is equivalent to # define macro in C language

-Dmacro=defn

It is equivalent to # define macro = defn in C language

-Umacro

Equivalent to # undef macro in C language

-undef

Remove the definition of any nonstandard macro

-Idir

When you use # “include” file “, GCC / G + + will first search the header file you set in the current directory. If it is not found, it will return to the default header file directory. If it uses – I to set the directory, it will first search in the directory you set, and then search in the normal order.

For # include, GCC / G + + will look up the directory specified by – I, if not, and then look up the default header file directory of the system.

-I-

It cancels the function of the previous parameter, so it is generally used after – idir.

-idirafter dir

Failed to search in – I directory. Let’s talk about searching in this directory.

-iprefix prefix 、-iwithprefix dir

Generally, it is used together. When the – I directory fails to find, it will be found under prefix + dir

-nostdinc

The compiler will no longer use the header file in the system default header file directory. It is generally used in combination with – I to explicitly limit the location of the header file.

-nostdin C++

This option is used in the libg + + library.

-C

When preprocessing, do not delete the comment information, generally use – E, sometimes the analyzer, use this very convenient.

-M

Generate information associated with the file. Contains all the source code that the object file depends on. You can test it with GCC – M hello. C, which is very simple.

-MM

Same as the one above, but it ignores the dependencies caused by # include.

-MD

Same as – m, but the output will be imported into the. D file

-MMD

Same as – mm, but the output will be imported into the. D file.

-Wa,opTIon

This option is passed to the assembler; if there is a comma in the middle of the option, the option is divided into multiple options and passed to the assembler.

-Wl.option

This option is passed to the linker; if there is a comma in the middle of the option, the option is divided into multiple options and passed to the linker.

-llibrary

Develop libraries to use when compiling

Example usage

gcc -lcurses hello.c

Compiler using ncurses Library

-Ldir

When compiling, search the path of the library. For example, you can use your own library to make a directory. Otherwise, the compiler will only search the directory of the standard library. This dir is the name of the directory.

-O0 、-O1 、-O2 、-O3

There are four levels of compiler optimization options, – o0 means no optimization, – O1 is the default value, – O3 is the highest optimization level.

-g

It’s just a compiler. When compiling, it generates debugging information.

-gstabs

This option claims debugging information in stabs format, but does not include GDB debugging information.

-gstabs+

This option claims debugging information in stabs format and contains additional debugging information for GDB use only.

-ggdb

This option will generate as much debugging information as possible for GDB.

-static

This option will prohibit the use of dynamic libraries, so the compiled things are generally very large and can run without any dynamic connection libraries.

-share

This option will use the dynamic library as much as possible, so the generated file is relatively small, but the system needs to be controlled by the dynamic library.

The above is the “C compiler” related content, I hope you can understand the common parameters of GCC C compiler through this article.

Editor in charge: PJ

Leave a Reply

Your email address will not be published. Required fields are marked *