C Programming in linux: Basic gcc usage

Source: Internet
Author: User
C Programming in linux started from 1: master the basic usage of gcc-general Linux technology-Linux programming and kernel information. The following is a detailed description. It is best to start with the command line at the beginning, so that you can familiarize yourself with the entire process of programming, compilation, debugging, and execution. You can write programs in vi or other editors.

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.

I. 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.

Ii. 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

Compared with the above two methods, the first method requires all files to be re-compiled during compilation, while the second method can only re-compile the modified files, and the unmodified files do not need to be re-compiled.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.