GCC的-g選項應該在編譯階段起作用(轉)
GCC的-g選項應該在編譯階段起作用(轉)[@more@][table=95%][tr][td]編譯過程:
原始碼.c -->預處理(把標頭檔案.h納入,預處理之後生成的是.i檔案) -->編譯(檢查正確後生成.s彙編程式碼檔案) -->彙編處理(將編譯階段生成的.s檔案轉換成目標檔案.o) -->連結(生成可執行檔案)
[/td][/tr][/table]上面是GCC編譯過程的分析。昨天練習建立多個檔案的project,但是寫好Makefile之後,使用GDB除錯時發現問題,具體如下:
[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]
也就是說無法list。原因是沒有加入-g選項。原來的Makefile檔案如下:
[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]那麼-g選項具體加在那個過程呢?實際上,-g選項應該在編譯階段加入,在生成的彙編程式碼檔案中就有了除錯資訊,否則就會出現start.S找不到的情況了。所以,修改了Makefile檔案如下:
[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]然後就可以除錯了,如下所示:
[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]----------------------------------------
附:例項原始碼
[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' into 'to';assume to is big enough
*/
void copy(char to[],char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '')
++i;
}
[/td][/tr][/table]
[table=95%][tr][td]/*
*getline.c
*read a line into s,return length
*/
#include <stdio.h>
int getline(char s[],int lim)
{
int c,i;
for (i = 0 ; i < lim-1 && (c = getchar()) != EOF && c != ' ' ; ++i)
s[i] = c;
if (c == ' ') {
s[i] = c;
++i;
}
s[i]='';
return i;
}
[/td][/tr][/table]
[table=95%][tr][td]/*
*main.c
*/
#include <stdio.h>
#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]這個project很短小,但是也說明了如何建立多個原始檔的project。其中,公用的宏定義、自定義的函式宣告、自定義的結構體變數都放在自定義標頭檔案裡。事先要規劃好程式結構!
原始碼.c -->預處理(把標頭檔案.h納入,預處理之後生成的是.i檔案) -->編譯(檢查正確後生成.s彙編程式碼檔案) -->彙編處理(將編譯階段生成的.s檔案轉換成目標檔案.o) -->連結(生成可執行檔案)
[/td][/tr][/table]上面是GCC編譯過程的分析。昨天練習建立多個檔案的project,但是寫好Makefile之後,使用GDB除錯時發現問題,具體如下:
[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]
也就是說無法list。原因是沒有加入-g選項。原來的Makefile檔案如下:
[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]那麼-g選項具體加在那個過程呢?實際上,-g選項應該在編譯階段加入,在生成的彙編程式碼檔案中就有了除錯資訊,否則就會出現start.S找不到的情況了。所以,修改了Makefile檔案如下:
[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]然後就可以除錯了,如下所示:
[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]----------------------------------------
附:例項原始碼
[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' into 'to';assume to is big enough
*/
void copy(char to[],char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '')
++i;
}
[/td][/tr][/table]
[table=95%][tr][td]/*
*getline.c
*read a line into s,return length
*/
#include <stdio.h>
int getline(char s[],int lim)
{
int c,i;
for (i = 0 ; i < lim-1 && (c = getchar()) != EOF && c != ' ' ; ++i)
s[i] = c;
if (c == ' ') {
s[i] = c;
++i;
}
s[i]='';
return i;
}
[/td][/tr][/table]
[table=95%][tr][td]/*
*main.c
*/
#include <stdio.h>
#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]這個project很短小,但是也說明了如何建立多個原始檔的project。其中,公用的宏定義、自定義的函式宣告、自定義的結構體變數都放在自定義標頭檔案裡。事先要規劃好程式結構!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-960165/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- GCC編譯選項GC編譯
- GCC 編譯選項GC編譯
- gcc或g++的編譯選項 -shared -fPIC 與 -g -rdynamic 部分轉載GC編譯
- gcc編譯階段列印巨集定義的內容GC編譯
- gcc常用的編譯選項對程式碼的影響(轉)GC編譯
- 用ccache加速你的gcc/g++編譯(轉)GC編譯
- cmake中新增 -g編譯選項編譯
- 編譯的各個階段編譯
- gcc最佳編譯引數(轉)GC編譯
- 安裝 GCC 編譯器(轉)GC編譯
- linux下gcc/g++編譯用法LinuxGC編譯
- gcc編譯GC編譯
- 【國外精選課程】編譯原理入門之編譯階段概述編譯原理
- FreeBSD中的GNU C編譯器--編譯器GCC(轉)編譯GC
- gcc 和 g++ 的聯絡和區別,使用 gcc 編譯 c++GC編譯C++
- GCC 使用指南及gcc最佳編譯引數(轉)GC編譯
- 【轉】gcc中的-w -W和-Wall選項GC
- PHP編譯選項PHP編譯
- GCC編譯器的使用GC編譯
- gcc編譯器小知識FAQ(轉)GC編譯
- 核心編譯選單中相關選項的意義(轉)編譯
- gcc 編譯器與 clang 編譯器GC編譯
- linux下的gcc編譯LinuxGC編譯
- 測試應該在產品開發的哪個階段進入?
- gcc編譯cpp檔案GC編譯
- vc 編譯連線選項編譯
- GCC/G++學習筆記 - 1 - 執行預編譯GC筆記編譯
- GNU 編譯器家族 GCC 內部探密(轉)編譯GC
- GCC編譯器背後的故事GC編譯
- VueJS 的編譯階段到掛載節點VueJS編譯
- 很有用的 GCC 命令列選項GC命令列
- c++進階(一)C語言條件編譯及編譯預處理階段C++C語言編譯
- gcc g++支援C++11 標準編譯及其區別GCC++編譯
- Linux上安裝GCC編譯器過程(轉)LinuxGC編譯
- Linux中gcc編譯工具LinuxGC編譯
- gcc 編譯多個原始檔GC編譯
- 記錄一次gcc的編譯GC編譯
- 編譯器GCC與Clang的異同編譯GC