gcc 的編輯方法

來源:互聯網
上載者:User
文章目錄
  • 交叉編譯工具簡介
  • c編譯語言的例子
  • 編譯動態檔案
  • 編譯成靜態檔案
  • 比較用不同的方便的大小的差異
  • gcc的使用機巧
 

GCC和交叉編譯中的工具的使用方法[編輯]

交叉編譯工具簡介

交叉工具鏈名稱

i386的工具名稱

歸屬

作用

arm-linux-as

as

binutils

編譯ARM組譯工具

arm-linux-ar

ar

binutils

把多個.o合并一個.o或是靜態庫(.a)

arm-linux-ranlib

ranlib

binutils

為庫檔案建立索引,想當於*.as -s

arm-linux-ld

ld

binutils

聯結器(Linker),把多個.o或是庫檔案連結一個可以執行檔案

arm-linux-objdump

objdump

binutils

查看目標檔案的(.o)或是庫(.a)的資訊

arm-linux-objcopy

objcopy

binutils

轉換成可以執行的ELF的檔案

arm-linux-strip

strip

binutils

去掉elf可以執行的資訊,是可以執行檔案變小

arm-linux-readelf

readelf

binutils

讀elf的檔案頭

arm-linux-gcc

gcc

gcc

編譯.c和.S的程式和彙編檔案

arm-linux-g++

g++

gcc

編譯個g++程式

[編輯]

c編譯語言的例子
分別通過gcc和交叉編譯的工具進行編譯下面的例子,分別用靜態編譯、編譯成動態庫和反組譯碼的方法。
[編輯]

hello_first.c
程式源碼:
#include <stdio.h>
void hello_first(void)

{

printf("first function /n");

}

[編輯]

hello_second.c
程式源碼:
#include <stdio.h>
void hello_second(void)
{
printf("second function /n");
}
[編輯]

hello.c
程式源碼:
#include <stdio.h>
void hello_first(void);
void hello_second(void);
int main(int argc,char *argv[])
{
hello_first();
hello_second();
}
[編輯]

編譯方法[編輯]

編譯動態檔案[編輯]

直接編譯[編輯]

方法一
#gcc -c hello_first.c
#gcc -c hello_second.c
#gcc -o hello hello.c hello_first.o hello_second.o
[編輯]

方法二
#gcc -c hello_first.c hello_second.c
#gcc -o hello hello.c hello_first.o hello_second.o
[編輯]

方法三
#gcc -c -o first.o hello_first.c
#gcc -c -o second.o hello_second.c
#gcc -o hello hello.c first.o second.o
[編輯]

方法四
#gcc -o hello hello.c hello_first.c hello_second.c
[編輯]

編譯動態庫進行編譯
#gcc -c -fpic hello_first.c hello_second.c
#gcc -shared hello_first.o hello_second.o -o hello.so
#gcc -o hello hello.c hello.so
/*可以把編譯的hello.so拷貝到/lib下或是設定環境變數LD_LIBRARY_PATH指向我們編譯的動態庫的
檔案位置,如,我們把hello.so拷貝到 /tmp下設定LD_LIBRARY_PATH=/tmp,然後重新執行
#./hello*/
[編輯]

編譯成靜態庫進行編譯
#gcc -c hello_first.c hello_second.c
/*建立一個靜態連結庫*/
#ar -r libhello.a hello_first.o hello_second.o
/*為靜態連結庫建立索引*/
#ar -s libhello.a
#ranlib libhello.a
/*由靜態庫編譯成可以執行的檔案*/
#gcc -o hello hello.c -lhello -L./
或是
#gcc -o hello hello.c libhello.a
/*編譯出來的hello可以脫離libhello.a執行*/
[編輯]

編譯成靜態檔案
#gcc -static -o hello hello.c hello_first.c hello_second.c
[編輯]

比較用不同的方便的大小的差異
編譯的大小比較
直接編譯

編譯成動態庫

編譯成靜態庫 

編譯成靜態檔案

[編輯]

gcc的使用機巧
在對mp3,video,bmp的檔案進行讀取的時候,會對檔案的頭資訊或是具體的結構進行讀取,
是可以通過進行結構 體進行讀取資訊,但是在正常編譯的時候(gcc),會出現記憶體的漏洞,
因為在正常定義的時候,編譯器會根據具體的系統結構是按整位元組處理,這樣我們提出的數值
就會出現問題,可以這樣解決。
例子:
#include <stdio.h>
int main(int argc,char **argv[])
{
struct test
{
int a;
char b[2];
int c;
};
printf("sizeof(struct test)=%d /n",sizeof(struct test));
}
輸出的結果並不是我們所需要的10byte,而是12byte,為什麼呢?因為在linux的
4個位元組對齊的。在第二個位元組裡面得就僅僅用了兩個位元組,可是在我們讀取的檔案信
息的時候就會出錯,可以這樣解決:
#include <stdio.h>
int main(int argc,char *argv[])
{
struct test
{
int a;
char b[2];
int c;
} __attribute__((packed));
printf("sizeof(struct test)=%d /n",sizeof(struct test))j;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.