// hello.c 練習用檔案
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("hello\n");
return 0;
}
gcc 階段
-E 僅作預先處理,不進行編譯、組譯和連結
-c 編譯、組譯到目的程式碼,不進行連結
-S 編譯到組合語言,不進行組譯和連結
ld 連結
$ gcc -E -o ./hello.i ./hello.c
會做預先處理, 看看 hello.i 就知道, 會把 #include 的檔案展開
$ gcc -o hello.o -c ./hello.c
會把c語言檔案編譯到 object code
$ file ./hello.o
出現
./hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
$ gcc -o hello.s -S ./hello.c
看看hello.s 到底是什麼樣子
.file "hello.c"
.section .rodata
.LC0:
.string "hello"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $4, %esp
movl $.LC0, (%esp)
call puts
movl $0, %eax
addl $4, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.2.2 20070909 (prerelease) (4.2.2-0.RC.1mdv2008.0)"
.section .note.GNU-stack,"",@progbits
$ gcc -o ./hello ./hello.c
編譯並連結 hello.c 產生 hello
看看編譯出來的檔案是哪種檔案
$ file ./hello
./hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), not stripped
可以看到這是 ELF 32 bit 的執行檔
for Intel 80386 的機器
動態連結
顯示 hello 需要哪些動態連結檔
$ ldd ./hello
linux-gate.so.1 => (0xfbffe000)
libc.so.6 => /lib/i686/libc.so.6 (0xb7e7e000)
/lib/ld-linux.so.2 (0xb7fcc000)
Powered by ScribeFire.
沒有留言:
張貼留言