The-g option of GCC should work in the compilation phase.

Source: Internet
Author: User
The-g option of GCC should take effect in the compilation phase-general Linux technology-Linux programming and kernel information. The following is a detailed description. [Table = 95%] [tr] [td] compilation process:
Source code. c --> preprocessing (put the header file. h is included, which is generated after preprocessing. I file) --> compile (check if the file is correct and generate it. s assembly code file) --> Assembly processing (will be generated in the compilation phase. s file to the target file. o) --> Link (generate executable files)
[/Td] [/tr] [/table] The above is an analysis of the GCC compilation process. Yesterday, I tried to create a project with multiple files, but after I wrote The Makefile, I found the problem during GDB debugging. The details are as follows:
[Table = 95%] [tr] [td] [armlinux @ lqm The-C-Programming-Language] $ cd example3
[Armlinux @ lqm example3] $ ls
Makefile copy. o getline. c main. c zifu
Copy. c gc. h getline. o main. o
[Armlinux @ lqm example3] $ gdb zifu
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu "...
(Gdb) l
1./sysdeps/i386/elf/start. S: No such file or directory.
In ../sysdeps/i386/elf/start. S
[/Td] [/tr] [/table]
That is to say, the list cannot be created. The reason is that the-g option is not added. The original Makefile file is as follows:
[Table = 95%] [tr] [td] CC = gcc

OBJS = main. o getline. o copy. o

Zifu: $ (OBJS)

$ (CC) $ ^-o $ @

Main. o: main. c gc. h

$ (CC)-c $ <

Getline. o: getline. c

$ (CC)-c $ <

Copy. o: copy. c

$ (CC)-c $ <

. PHONY: clean

Clean:

Rm-f $ (OBJS) zifu


[/Td] [/tr] [/table] what is the-g option used in that process? In fact, the-g option should be added in the compilation phase, and debugging information is available in the generated assembly code file; otherwise, the start. S cannot be found. Therefore, the Makefile file is modified as follows:

[Table = 95%] [tr] [td] CC = gcc

OBJS = main. o getline. o copy. o

Zifu: $ (OBJS)

$ (CC) $ ^-o $ @

Main. o: main. c gc. h

$ (CC)-g-c $ <

Getline. o: getline. c

$ (CC)-g-c $ <

Copy. o: copy. c

$ (CC)-g-c $ <

. PHONY: clean

Clean:

Rm-f $ (OBJS) zifu


[/Td] [/tr] [/table] Then you can debug it, as shown below:

[Table = 95%] [tr] [td] [armlinux @ lqm example3] $ gdb zifu
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu "...
(Gdb) l
9
10/* print the longest input line */
11 int main ()
12 {
13 int len;/* current line length */
14 int max;/* maximum length seen so far */
15 char line [MAXLINE];/* current input line */
16 char longest [MAXLINE];/* longest line saved here */
17
18 max = 0
[/Td] [/tr] [/table] ------------------------------------------
Appendix: instance source code
[Table = 95%] [tr] [td] [armlinux @ lqm example3] $ tree
.
| -- Makefile
| -- Copy. c
| -- Gc. h
| -- Getline. c
'-- Main. c

0 directories, 5 files
[/Td] [/tr] [/table]
[Table = 95%] [tr] [td]/*
* Copy. c
* Copy 'from 'input' to '; assume to is big enough
*/

Void copy (char to [], char from [])
{
Int I;

I = 0;
While (to [I] = from [I])! = '\ 0 ')
++ I;
}
[/Td] [/tr] [/table]
[Table = 95%] [tr] [td]/*
* Getline. c
* Read a line into s, return length
*/

# Include

Int getline (char s [], int lim)
{
Int c, I;

For (I = 0; I <lim-1 & (c = getchar ())! = EOF & c! = '\ N'; ++ I)
S [I] = c;
If (c = '\ n '){
S [I] = c;
++ I;
}
S [I] = '\ 0 ';
Return I;
}
[/Td] [/tr] [/table]
[Table = 95%] [tr] [td]/*
* Main. c
*/

# Include
# Include "gc. h"

# Define MAXLINE 1000/* maximum input line length */

/* Print the longest input line */
Int main ()
{
Int len;/* current line length */
Int max;/* maximum length seen so far */
Char line [MAXLINE];/* current input line */
Char longest [MAXLINE];/* longest line saved here */

Max = 0;
While (len = getline (line, MAXLINE)> 0)
If (len> max ){
Max = len;
Copy (longest, line );
}
If (max> 0)
Printf ("% s", longest );
Return (0 );
}
[/Td] [/tr] [/table]
[Table = 95%] [tr] [td]/*
* Gc. h
*/
Int getline (char [], int );
Void copy (char [], char []);
[/Td] [/tr] [/table] This project is short and small, but it also shows how to create a project with multiple source files. The public macro definition, custom function declaration, and custom struct variables are all placed in the Custom header file. Plan the program structure in advance!
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.