How to compile c language code in centos

王林
Release: 2020-05-20 14:27:58
Original
8218 people have browsed it

How to compile c language code in centos

Compile c, c code

Install gcc

1. Use the following command to query centos official gcc All packages:

yum -list gcc*
Copy after login

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
Copy after login

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
Copy after login

Use gcc:

The sample program is as follows:

//test.c
#include <stdio.h>
int main()
{
    printf("Hello World!\n");
    return 0;
}
Copy after login

Compilation method:

Assume that the source program file is named test.c

1. Compile and link without options

#gcc test.c
Copy after login

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
Copy after login

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
Copy after login

Function: Preprocess test.c and output the test.i file.

4. Option -S

#gcc -S test.i
Copy after login

Function: Assemble the preprocessing output file test.i into the test.s file.

5. Option -c

#gcc -c test.s
Copy after login

Function: Compile the assembly output file test.s and output the test.o file.

6. Link without options

#gcc test.o -o test
Copy after login

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
Copy after login

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++
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template