自分で作ったり提供したりするものは、まず自分自身で使ってみろろということです。自分じゃ使わないものなら人はいくらでも無責任にも無思考にもなれる。そういう投げやりな「サービス」やら「プロダクツ」なんて、だれだってイヤだ。自分が作り手と同時に利用者の立場になれば、ちゃんと使えるレベルのものを提供しようとします。

2010年9月14日火曜日

Cコンパイルの過程

Cプログラムをコンパイルする四つの過程を紹介します。

①予備処理→②コンパイル→③アセンブラ→④リンク

具体例:hello.c
#include <stdio.h>
int main( int argc, char *argv[] )
{
    printf( "Hello World!\n" );
   
    return 0;
}

①予備処理
gcc -E hello.c -o hello.i

②コンパイル
gcc -S hello.i -o hello.s

③アセンブラ
gcc -c hello.s -o hello.o

④リンク
gcc hello.o -o hello.exe

ファイル詳細:
hello.i
# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "hello.c"
# 1 "c:\\gnustep\\mingw\\bin\\../lib/gcc/mingw32/4.4.0/../../../../include/stdio.h" 1 3
# 19 "c:\\gnustep\\mingw\\bin\\../lib/gcc/mingw32/4.4.0/../../../../include/stdio.h" 3
# 1 "c:\\gnustep\\mingw\\bin\\../lib/gcc/mingw32/4.4.0/../../../../include/_mingw.h" 1 3
# 31 "c:\\gnustep\\mingw\\bin\\../lib/gcc/mingw32/4.4.0/../../../../include/_mingw.h" 3
      
# 32 "c:\\gnustep\\mingw\\bin\\../lib/gcc/mingw32/4.4.0/../../../../include/_mingw.h" 3
# 20 "c:\\gnustep\\mingw\\bin\\../lib/gcc/mingw32/4.4.0/../../../../include/stdio.h" 2 3

…省略

hello.s
    .file    "hello.c"
    .def    ___main;    .scl    2;    .type    32;    .endef
    .section .rdata,"dr"
LC0:
    .ascii "Hello World!\0"
    .text
.globl _main
    .def    _main;    .scl    2;    .type    32;    .endef
_main:
    pushl    %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $16, %esp
    call    ___main
    movl    $LC0, (%esp)
    call    _puts
    movl    $0, %eax
    leave
    ret
    .def    _puts;    .scl    2;    .type    32;    .endef

0 件のコメント:

コメントを投稿

ホームページ