GCC, a well-known compiler software, must be known if you use Unix systems, including Linux.
Gcc-GNU Project C and C ++ Compiler
1. Basic usage
GCC is generally used as the C language compiler, and g ++ is used as the C ++ language compiler.
Its syntax structure is:
GCC [-c |-S |-E] [-STD = standard] [-G] [-PG] [-olevel] [-Wwarn...] [-pedantic] [-Idir...] [-ldir...] [-Dmacro [= defn]...] [-umacro] [-Foption...] [-mmachine-option...] [-O outfile] infile... |
Although it seems that there are many options, it seems very advanced. However, only one infile is required. Therefore, we only need to know this form for beginners:
GCC source-Files
You can use any text compiler to compile a C language source program and save it as a text file in UNIX format. For example, the file name is test. c, then use the following command to turn the source program into an executable program:
GCC test. c
If you are using a source program written by the file editing software in Windows, you 'd better use the command dos2unix to convert the file format before compiling with GCC. Because earlier GCC versions do not recognize the file format in windows. A strange error is reported.
The dos2unix command is provided by the software package tofrodos. The package information is as follows:
Description: converts dos <-> UNIX text files, alias tofromdos DOS text files traditionally have Cr/LF (carriage return/line feed) pairs As their new line delimiters while UNIX text files traditionally have LFS (line feeds) to terminate each line. . Tofrodos comprises one program, "fromdos" alias "Todos", which converts Text files to and from these formats. Use "fromdos" to convert DoS Text files to the Unix format, and "Todos" to convert UNIX text files To the DOS format. . This functionality is also available via the dos2unix/unix2dos symlinks. . Homepage: http://www.thefreecountry.com/tofrodos/index.shtml |
Converts the Windows text file format and the Linux text file format.
Windows text files use the CR and LF line terminator, while Linux text files use lf as the line terminator. Therefore, when a text file written in Linux is opened with notepad or other software in windows, all the content is squeezed in one line.
In C language, the row terminator of a text file in Windows is '/R' and'/N', and in Linux is '/N ', in Windows, 0x0d and 0x0a are in hexadecimal notation, while in Linux, 0x0a is in hexadecimal notation.
Use the following compilation command:
GCC test. c
An executable program A. out will be generated in the current directory. How can I run the program? Enter the following command:
./A. Out
If you want to generate a program with your own command, that is, the program name is not the default A. Out, you can use this command format:
Gcc-O out-file source-File
For example:
Gcc-O myprog test. c
Turn the source program test. c into an executable program myprog. Run this command to see your results:
./Myprog
2. Compile multiple files
If your program has a lot of source code and is divided into several source files and called before each other, you need to use GCC to compile multiple source files. The command format is as follows:
Gcc-O out-file source-file-a source-file-B Source-file-c...
Compile all source code files to generate a program out-file.
3. Code style
Many people will talk about the code style, but this should be what machines do, not what people do!
In Linux, there is a good software indent that can be used to sort the code according to a certain style. The package information is as follows:
Description: C language source code formatting Program The 'indent 'program changes the appearance of a C program Inserting or deleting whitespace. . 'Indent 'also provides options for controlling the alignment of braces and Declarations, program indenting, and other stylistic parameters, including Formatting of both C and C ++ comments. |
For example, we have compiled a source program test. C. The code format is messy, as shown below:
# Include <stdio. h> # Include <string. h> # Include <stdlib. h>Int main (INT argc, char ** argv) { Int Port = 0; Printf ("Program begin/N "); If (argc! = 3 ){ Printf ("the number of program parameters is incorrect! Correct Input Method:/n % s IP-address port/N ", argv [0]); Exit (0 ); } If (atoi (argv [2]) <0 | atoi (argv [2])> 65535) {printf ("port input is incorrect! It should be a value between 0 and. The default value of the program is 3456/N "); Port = 3456 ;} Else Port = atoi (argv [2]); Printf ("command: % S % d/N", argv [0], argv [1], Port ); Return 0; } |
Use the indent command to format it. The command is as follows:
Indent test. c
The source code is as follows:
# Include <stdio. h> # Include <string. h> # Include <stdlib. h>Int Main (INT argc, char ** argv) { Int Port = 0; Printf ("Program begin/N "); If (argc! = 3) { Printf (" Program parameters The number is incorrect! The correct input method is as follows:/n % s IP-address port/N ", Argv [0]); Exit (0 ); } If (atoi (argv [1]) <0 | atoi (argv [2])> 65535) { Printf ("Port input is incorrect! It should be a value between 0 and. The default value is 3456/N "); Port = 3456; } Else Port = atoi (argv [2]); Printf ("command: % S % d/N", argv [0], argv [1], Port ); Return 0; } |
This code style is GNU and the default format of indent.
However, most of our programmers may learn the kernighan & Ritchie code style and use the following command to format it:
Indent-Kr test. c
The code is formatted as follows:
# Include <stdio. h> # Include <string. h> # Include <stdlib. h>Int main (INT argc, char ** argv) { Int Port = 0; Printf ("Program begin/N "); If (argc! = 3 ){ Printf (" Program The number of parameters is incorrect! Correct Input The method is as follows:/n % s IP-address port/N ", Argv [0]); Exit (0 ); } If (atoi (argv [2]) <0 | atoi (argv [2])> 65535 ){ Printf ("Port input is incorrect! It should be a value between 0 and. The default value is 3456/N "); Port = 3456; } Else Port = atoi (argv [2]); Printf ("command: % S % d/N", argv [0], argv [1], Port ); Return 0; } |
Remember: Do not do things that should have been done by machines manually!
Of course, if you use indent formatting to report an error or find something wrong after formatting, it must be a problem in your code, such as missing "}"
4. develop good habits
Everyone writes C Programs, but there will always be problems with some code, and if you write better, there will be basically no errors.
This requires a process of practice, but as a beginner, you should add the GCC-wall option when compiling the program. The command format is:
Gcc-wall-O out-file source-File
In this way, there is a prompt when there is a problem with each line of code in the program. It is quite useful for beginners to figure out every warning and error to develop good programming habits.
5. Prepare the debugging program
We may have compiled and generated executable programs, but may encounter errors during the running, or even a problem occurs one day after a period of time.
If you have any questions, see the error prompt. But some may not be clear for a long time, so they can only debug the program.
To ensure that the program can be debugged, we must add the-G option when compiling the program with GCC, that is, the command format is as follows:
Gcc-wall-g-o out-file source-File
This command adds debugging information to the executable program generated by source code compilation, if a program error occurs in the core dump file, you can quickly locate the program line from the core dump file.