Installing GCC in Ubuntu is a little different from other Linux systems. 1) install vim) open the command line to run sudoapt-getinstallvim, and enter the administrator password as prompted. Gcc + gdb) enter the command line to run sudoapt-getinstallbuild-essential build-essential, including tools such as gcc and gdb.
Installing GCC in Ubuntu is a little different from other Linux systems.
1) installation
Run sudo apt-get install vim on the command line, and enter the administrator password as prompted.
Gcc + gdb) input the command line to run sudo apt-get install build-essential
Build-essential includes tools such as gcc and gdb, and is a C language development kit.
After installation, you can execute
Gcc -- version
Command to view the version. the output is as follows:
Gcc (GCC) 4.2.3 (Ubuntu4.2.3-2ubuntu7)
Copyright (C) 2007 FreeSoftwareFoundation, Inc.
The gcc command is used for compiling. First, you must be familiar with the usage of gcc commands.
Gcc commands provide many command options, but not all of them should be familiar with them. you can master several common options at the beginning, and then learn other options later, in this case, too many options may affect your learning confidence.
2) Common compilation command options
Assume that the source program file name is test. c.
1. no option to compile the link
Usage: # gcc test. c
Purpose: Prepare, compile, and link test. c to form an executable file. The output file is not specified here. the default output is a. out.
2. option-o
Usage: # gcc test. c-o test
Purpose: pre-process, compile, compile, and link test. c to form the executable file test. The-o option is used to specify the name of the output file.
3. option-E
Usage: # gcc-E test. c-o test. I
Purpose: preprocess test. c to output the test. I file.
4. option-S
Usage: # gcc-S test. I
Purpose: compile the pre-processing output file test. I into the test. s file.
5. option-c
Usage: # gcc-c test. s
Purpose: compile the output file test. s and output the file test. o.
6. no option link
Usage: # gcc test. o-o test
Purpose: link the compiled output file test. oto the final executable file test.
7. option-O
Usage: # gcc-O1 test. c-o test
Purpose: use compile optimization level 1 to compile the program. Level 1 ~ 3. the higher the level, the better the optimization effect, but the longer the compilation time.
3) multi-source file compilation method
If there are multiple source files, there are basically two compilation methods:
[Assume there are two source files: test. c and testfun. c]
1. compile multiple files together
Usage: # gcc testfun. c test. c-o test
Purpose: compile testfun. c and test. c respectively and link them to the test executable file.
2. compile each source file separately, and then link to the target file output after compilation.
Usage:
# Gcc-c testfun. c // compile testfun. c into testfun. o
# Gcc-c test. c // compile test. c into test. o
# Gcc-o testfun. o test. o-o test // link testfun. o and test. Oto test
4) use of the gdb debugger
Run gdb Cfilename to Start program debugging.
The following lists some common gdb commands:
L Output source code from line n to line n + 9
Break Set a breakpoint in line n
Info break
R running
N single-step execution
C. continue execution
P varName output variable value
Q exit