Compile c, c code
Install gcc
1. Use the following command to query centos official gcc All packages:
yum -list gcc*
Installable software packages
gcc.x86_64 gcc-c++.x86_64 gcc-gfortran.x86_64 gcc-gnat.x86_64 gcc-go.x86_64 gcc-objc.x86_64 gcc-objc++.x86_64 gcc-plugin-devel.x86_64
2. Install packages as needed, edit c, c needs to install gcc.x86_64 and gcc-c .x86_64
yum -y install gcc.x86_64 yum -y install gcc-c++.x86_64
Use gcc:
The sample program is as follows:
//test.c #include <stdio.h> int main() { printf("Hello World!\n"); return 0; }
Compilation method:
Assume that the source program file is named test.c
1. Compile and link without options
#gcc test.c
Function: Preprocess, assemble, compile and link test.c to form an executable file. No output file is specified here, the default output is a.out. After the compilation is successful, 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 name of the executable program file.
2. Option -o
#gcc test.c -o test
Function: Preprocess, assemble, compile and link test.c to form the 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
#gcc -E test.c -o test.i
Function: Preprocess test.c and output the test.i file.
4. Option -S
#gcc -S test.i
Function: Assemble the preprocessing output file test.i into the test.s file.
5. Option -c
#gcc -c test.s
Function: Compile the assembly output file test.s and output the test.o file.
6. Link without options
#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.
If you want to directly enter test and run it, you need to copy test to the directory /usr/bin.
7. Option -O
#gcc -O1 test.c -o test
Function: Use compilation 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 programs using C std library
#gcc test.cpp -o test -l std c++
Function: Compile and link test.cpp into a test executable file. -l std c specifies linking std c library.
Recommended tutorial: centos tutorial
The above is the detailed content of How to compile c language code in centos. For more information, please follow other related articles on the PHP Chinese website!