Multiple source files compiled by GCC

Source: Internet
Author: User

I. Common compilation Command Options
Assume that the sourceProgramThe file name isTest. 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.

3. If the files to be compiled are all in the same directory, you can use the wildcard GCC *. C-O for compilation.

Are you sure you want to ask, if it is a project, there may be hundreds of files. Isn't this compilation method exhausting people in front of the computer, or when you compile it successfully, isn't it because your hair is white? Well, we need to write the above compilation process into the next text file:
Makefile in Linux

# Here are some file descriptions.
Myfirst: myfirst. O hello. o
G ++ myfirst. O hello. O-o myfirst
Hello. O: Hello. cpp
G ++-C hello. cpp-O hello. o
Myfirst. O: myfirst. cpp
G ++-C myfirst. cpp-O myfirst. o

Makefile writing rules:
(1) behavior comments starting "#"
(2) The file dependency is:
Target: Components
Rule

If the disk is saved as myfirst, input "make myfist" on the terminal. If a program error occurs, it is the enemy of all programmers. When writing a program, we should try our best to avoid the error, however, when writing the program, it is inevitable that such errors occur. It is a good idea to debug the program as necessary. How can we debug the program? See the following:

GDB. /file name // here I want to modify it to debug it. The parameter G must be added during compilation, g ++-G hello. CPP-O hello

The following are the commands that can be used in the debugging status (only word input can be entered, for example, break can be simplified to B ).

List <displaySource code>
Break row number <set breakpoint>
Run <running program>
Continue <continue from the breakpoint>
Print variable <view the variable value during debugging>
Del row number <Delete breakpoint>
Step <one-step execution, which can be tracked inside the function>
Next <single-step execution, cannot be tracked inside the function>
Quit <exit>

Makefile writing is not easy, because self-written makefile may not be applicable in all UNIX/Linux operating systems. Therefore, tools such as automake. Autoconf or cmake are used in many projects.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/niuniuchou/archive/2010/03/18/5394210.aspx

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.