Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手

先決條件

gcc:C 語言編譯器。我曾經寫了一支 Linux 多執行緒程式 Main.c,用 gcc -g Main.c -lpthread -o Main 編譯時跳出未安裝 gcc 的提示,照著輸入幾個 y 就裝好了。其實在 Red Hat Enterprise Linux 6 上預設已安裝 gcc,可以直接編譯 C 程式。

gcc-c++:把 gcc 擴充為 C++ 編譯器。這個套件為 GNU C 編譯器加上 C++ 支援,涵蓋當時 C++ 標準的大部分特性(含樣板與例外處理),但不含標準 C++ 函式庫。

libstdc++-v3:GNU 標準 C++ 函式庫 v3,隨 GCC 原始碼一起發布,不再單獨推出快照。

一、gcc 與 g++ 的編譯流程

預處理 → 編譯 → 匯編 → 連結。

先看原始碼 hello.c:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
#include <stdio.h>
int main(){
    printf("in C");
    return 0;
}

(0)一步到位

[hadoop@sam1 testGCC]$ gcc hello.c -o hello
[hadoop@sam1 testGCC]$ ./hello
in C

加上 -O 選項可讓 GCC 做基本優化:

gcc hello.c -o hello -O0   # 無優化
gcc hello.c -o hello -O1   # 預設,主要做跳轉與延遲退棧兩種優化
gcc hello.c -o hello -O2   # 在 -O1 之外再做額外指令調整
gcc hello.c -o hello -O3   # 在 -O2 之外再進行循環展開等優化

(1)預處理:hello.c → hello.i。預處理器的輸出預設送到標準輸出,所以要自行重定向:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
[hadoop@sam1 testGCC]$ gcc -E hello.c -o hello.i

.c 副檔名代表需要預處理的 C 原始碼;-E 表示做完預處理就停,不執行真正的編譯。

(2)編譯:hello.i → hello.s,產生組合語言。

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
[hadoop@sam1 testGCC]$ gcc -S hello.i -o hello.s

產出的組合語言大致如下:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
    .file   "hello.c"
    .section .rodata
.LC0:
    .string "in C"
    .text
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $16, %esp
    movl    $.LC0, %eax
    movl    %eax, (%esp)
    call    printf
    movl    $0, %eax
    leave
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
    .section .note.GNU-stack,"",@progbits

-S 表示編譯完成就停,不進行匯編。

(3)匯編:hello.s → hello.o。

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
[hadoop@sam1 testGCC]$ gcc -c hello.s -o hello.o

-c 表示編譯或匯編但不連結,輸出目的檔。

(4)連結:hello.o → 可執行檔 hello。

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
[hadoop@sam1 testGCC]$ gcc hello.o -o hello
[hadoop@sam1 testGCC]$ ./hello
in C

小結這幾個參數的意義:它們指定編譯在哪個階段停下,與輸入檔的格式無關。-E 停在預處理,-S 停在產生組合語言,-c 停在產生目的檔,不帶參數則一路做到連結完成。

另外 gcc -g 會在目的檔中塞入除錯資訊,表現為 ELF 檔多出許多 debug 段,可用 readelf -S ***.o 查看:

[27] .debug_aranges  PROGBITS 00000000 0006e4 000020 00 0 0 1
[28] .debug_pubnames PROGBITS 00000000 000704 00004f 00 0 0 1
[29] .debug_info     PROGBITS 00000000 000753 0000cb 00 0 0 1
[30] .debug_abbrev   PROGBITS 00000000 00081e 00008d 00 0 0 1
[31] .debug_line     PROGBITS 00000000 0008ab 000046 00 0 0 1
[32] .debug_frame    PROGBITS 00000000 0008f4 000054 00 0 0 4
[33] .debug_str      PROGBITS 00000000 000948 000095 01 MS 0 0 1

二、用 Makefile 管理一個小專案

專案含 3 個 .cpp 與 2 個 .h:

main.cpp:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
#include <iostream>
#include "printf1.h"
#include "printf2.h"
int main(){
    printf1();
    printf2();
}

printf1.h:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
void printf1();

printf1.cpp:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
#include <iostream>
#include "printf1.h"
using namespace std;
void printf1(){
    cout << "printf1" << endl;
}

printf2.h:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
void printf2();

printf2.cpp:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
#include <iostream>
#include "printf2.h"
using namespace std;
void printf2(){
    cout << "printf2" << endl;
}

常規編譯

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
[hadoop@sam1 testGCC]$ g++ -c printf1.cpp   # 產生 printf1.o
[hadoop@sam1 testGCC]$ g++ -c printf2.cpp
[hadoop@sam1 testGCC]$ g++ -c main.cpp
[hadoop@sam1 testGCC]$ g++ printf1.o printf2.o main.o -o out
[hadoop@sam1 testGCC]$ ./out
printf1
printf2

改用 Makefile

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
[hadoop@sam1 testGCC]$ vim makefile
[hadoop@sam1 testGCC]$ make
g++ -c main.cpp
g++ -c printf1.cpp
g++ -c printf2.cpp
g++ main.o printf1.o printf2.o -o out
[hadoop@sam1 testGCC]$ ./out
printf1
printf2
[hadoop@sam1 testGCC]$ make clean
rm -rf *.o out

Makefile 內容:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
out: main.o printf1.o printf2.o
    g++ main.o printf1.o printf2.o -o out

main.o: main.cpp printf1.h printf2.h
    g++ -c main.cpp

printf1.o: printf1.h printf1.cpp
    g++ -c printf1.cpp

printf2.o: printf2.h printf2.cpp
    g++ -c printf2.cpp

clean:
    rm -rf *.o out

三、用 GDB 除錯

參考資料:IBM developerWorks 的《使用 GDB 調試 Linux 軟件》,以及 LINUX GDB 調試實例(https://blog.csdn.net/wangjiannuaa/article/details/6584750)。

下面用一個緩衝區溢出的例子,示範如何用 gdb 觀察執行期堆疊:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手
#include <stdio.h>
void why_here(void){
    printf("whyuhere?!\n");
    _exit(0);
}
int main(int argc, char *argv[]){
    int buff[1];
    buff[2] = (int)why_here;
    return 0;
}
gcc -g buf.c -o buf

啟動 gdb:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手

在程式起點設中斷點(這裡實際只設了一個,因為定義變數不會出現在指令序列中):

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手

讓程式跑到中斷點:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手

查看 main 函式的堆疊資訊:

Linux 開發環境建置:gcc/g++、Makefile 與 GDB 一站式上手

遇到函式時,單步進入用 step,單步跳過用 next。

延伸閱讀:https://www.seas.upenn.edu/cets/answers/gcc.html

Copyright © Linux教程網 All Rights Reserved