Use gcc command to compile code under linux

This article mainly introduces the basic usage of gcc, first master the basic usage, and later master the advanced instructions (such as -lpthread and other library connections)

It is best to start from the command line at the beginning, so that you can be familiar with the entire process of writing, compiling, debugging and executing. Programs can be written with vi/vim (I personally think vim is better than vi) or other editors.
To compile, use the gcc command. To learn further, you must first be familiar with the usage of gcc commands.
 The gcc command provides a lot of command options, but not all of them need to be familiar. It is enough to master a few commonly used at the beginning, and then slowly learn other options later, lest the confidence of learning is affected by too many options.
1. Commonly used compilation command options
Assuming the source file name is test.c
1. Compile and link without options
Usage: #gcc test.c
Function: preprocess, assemble, compile and link test.c to form an executable file. No output file is specified here, and the default output is a.out. After successful compilation, you can see that an a.out file is generated. Enter ./a.out on the command line to execute the program. ./ means in the current directory, a.out is the executable program file name.
2. Option -o
usage: #gcc test.c -o test
Function: preprocess, assemble, compile and link test.c to form an executable file test. The -o option is used to specify the file name of the output file. Enter ./test to execute the program.
3. Option -E
usage: #gcc -E test.c -o test.i
Function: preprocess test.c and output test.i file.
4. Option -S
usage: #gcc -S test.i
Function: Assemble the preprocessed output file test.i into a test.s file.
5. Option -c
usage: #gcc -c test.s
Function: compile the assembly output file test.s and output the test.o file.
6. Link without option
Usage: #gcc test.o -o test
Function: Link the compiled output file test.o into the final executable file test. Enter ./test to execute the program.
7. Option -O
usage: #gcc -O1 test.c -o test
Function: Use compiler optimization level 1 to compile the program. The level is 1~3, the larger the level, the better the optimization effect, but the longer the compilation time. Enter ./test to execute the program.
8. Compile the program using the C++ std library.
Usage: #gcc test.cpp -o test -lstdc++
Function: compile and link test.cpp into a test executable file. -lstdc++ specifies to link the std c++ library.
2. Compilation method
of multiple source files 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
Function: compile testfun.c and test.c respectively and link them into test executable files.
2. Compile each source file separately, and then link the object file output after compilation.
Usage:
#gcc -c testfun.c //Compile testfun.c to testfun.o
#gcc -c test.c //Compile test.c to test.o
#gcc -o testfun.o test.o -o test //
    Compare the above two methods by linking testfun.o and test.o to test . The first method requires all files to be recompiled when compiling, while the second method You can only recompile the modified files, and the unmodified files do not need to be recompiled.

Guess you like

Origin blog.csdn.net/qq_37061368/article/details/112304380