Linux GCC methods for compiling multiple files

Source: Internet
Author: User

Suppose the source program file is named TEST.c.

1. No option to compile links
Usage: #gcc test.c
Role: Pre-test.c, assemble, compile, and link the executable file. The output file is not specified here, and the default output is a.out.

2. Option-O
Usage: #gcc test.c-o test
Role: preprocessing, assembling, compiling, and linking test.c to form the executable file test. The-o option is used to specify the file name of the output file.

3. Option-E
Usage: #gcc-e test.c-o test.i
Function: The test.c preprocessing output test.i file.

4. Option-S
Usage: #gcc-S TEST.I
Function: test.i the preprocessed output file into a Test.s file.

5. Option-C
Usage: #gcc-C TEST.S
Function: Compiles output file Test.s compiled output TEST.O file.

6. No option link
Usage: #gcc test.o-o test
Function: Link the compiled output file TEST.O to the final executable test.

7. Option-O
Usage: #gcc-o1 test.c-o Test
Role: Compile the program using the Compile Optimization level 1. The higher the level, the better the optimization effect, but the longer the compilation time.

Two. Multi-source file compilation method

If you have more than one source file, there are basically two ways to compile it:
[Suppose there are two source files for TEST.c and TESTFUN.C]

1. Compiling multiple files together
Usage: #gcc testfun.c test.c-o Test
Role: TESTFUN.C and TEST.c are compiled and linked into Test executables.

2. Compile each source file separately, then link to the target file for post-compilation output.
Usage:
#gcc-C TESTFUN.C//TESTFUN.C compiled into TESTFUN.O
#gcc-C test.c//test.c compiled into TEST.O
#gcc-o testfun.o test.o-o Test//link TESTFUN.O and TEST.O to test

The above two methods compare, the first method compiles all the files need to recompile, and the second method can only recompile modified files, unmodified files do not have to recompile.

3. if the files to be compiled are in the same directory, you can compile them with the wildcard GCC *.c-o.

You will ask, if it is a project, there may be hundreds of documents, such a compilation method, the person is not to be exhausted in front of the computer, or until you compile successfully, not the hair is white, hehe, so we have to write the above compilation process into the following text file:
Called Makefile under Linux

#这里可以写一些文件的说明
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) Comment on behavior starting with "#"
(2) file dependencies are:
Target:components
Rule

Save for Myfirst, in the terminal input: Make myfist, the program has been wrong but all the programmers common enemy, in writing the program we should try to avoid the error, but when writing, how inevitably the occurrence of such a mistake, the program It's a good idea to do the necessary debugging, so how do we debug the program, see below:

GdB./File name////////////////Here I modify the following to be able to debug, in the compilation above must add parameters g,g++-G hello.cpp-o Hello

The following are the commands you can use in the debug state (you can enter only the word input, such as break to B), and the angle brackets for the description

List < show source code >
Break line number < set breakpoints >
Run < running programs >
Continue < Continue execution from Breakpoint >
Print variables < View values for variables when debugging >
Del Line number < delete breakpoint >
Step < step, trace to function inside >
Next < Single step, non-traceable to function inside >
Quit < exit >

Makefile writing is not an easy task, because the makefile you write may not be common to all Unix/linux-class operating systems. As a result, automake.autoconf or CMake tools are used in many projects.

Linux GCC methods for compiling multiple files

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.