美文网首页
GCC 中文手册 (1)源代码->可执行文件

GCC 中文手册 (1)源代码->可执行文件

作者: 极客圈 | 来源:发表于2017-04-02 10:58 被阅读0次

    本手册页内容摘自 GNU C 编译器的完整文档,仅限于解释选项的含义.
    如果发现手册页和Info 文件之间有所矛盾,请查对 Info 文件(权威文档)

    GCC

    gcc GNU 工程的 C 编译器
    g++ GNU 工程的 C++编译器

    gcc[option|filename ]...
    g++[option|filename ]...

    描述(DESCRIPTION)

    C 和 C++编译器是集成的
    都要用4个步骤中的一个或多个处理输入文件:
    预处理 (preprocessing)      cpp -Egcc -E
    编译(compilation)        cc1 -Sgcc -S
    汇编(assembly)          as
    链接(linking)          ld

    源文件后缀名标识源文件的语言,但是对编译器来说,后缀名控制着缺省设置:
    • gcc 认为预处理后的文件(.i)是 C 文件,并且设定 C 形式的连接.

    • g++ 认为预处理后的文件(.i)是 C++文件,并且设定 C++形式的连接.

    源文件后缀名指出语言种类以及后期的操作:

    .c       C源程序        预处理,编译,汇编
    .C      C++源程序       预处理,编译,汇编
    .cc      C++源程序       预处理,编译,汇编
    .cxx      C++源程序       预处理,编译,汇编
    .m       Objective-C源程序   预处理,编译,汇编
    .i       预处理后的C文件     编译,汇编
    .ii      预处理后的C++文件   编译,汇编
    .s      汇编语言源程序     汇编
    .S      汇编语言源程序     预处理,汇编
    .h      预处理器文件      通常不出现在命令行上

    其他后缀名的文件被传递给连接器(linker).通常包括:

    .o     目标文件(Object file)
    .a     归档库文件(Archive file)

    除非使用了-c, -S,或-E 选项(或者编译错误阻止了完整的过程),否则连接总是最后的步骤.
    在连接阶段中,所有对应于源程序的.o 文件, -l 库文件,无法识别的文件名(包括指定的 .o 目 标文件和.a 库文件)按命令行中的顺序传递给连接器.

    例子

    hello.c源代码:

    #include<stdio.h>  
      
    int main()  
    {  
          printf("Hello! This is our embedded world!\n");  
      
          return 0;  
    }  
    

    (1)预处理阶段
    预处理阶段 主要处理#include和#define

    • #include编译器把#include.h文件 包含到源代码中
    • #define定义的 源代码中实际使用到的 宏 用实际的字符串代替

    预处理阶段 可理解为简单的文本相加/替换:
    hello.c + .h文件源码 替换宏定义define ---> hello.i

    gcc命令的一般格式

    gcc [选项] 要编译的文件 [选项] [目标文件]
    ( -o 参数省略时,Gcc默认生成可执行的文件:编译文件.out )

    例子
    Gcc –E hello.c –o hello.i
    gcc -E 表示只进行预处理(不进行后面的3个阶段)
    -o 指目标文件(gcc输出的文件)
    .i 文件为已经过预处理的C原始程序 hello.i的内容如下:

    # 1 "hello.c"
    # 1 "<built-in>" 1
    # 1 "<built-in>" 3
    # 330 "<built-in>" 3
    # 1 "<command line>" 1
    # 1 "<built-in>" 2
    # 1 "hello.c" 2
    # 1 "/usr/include/stdio.h" 1 3 4
    # 64 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/sys/cdefs.h" 1 3 4
    # 587 "/usr/include/sys/cdefs.h" 3 4
    # 1 "/usr/include/sys/_symbol_aliasing.h" 1 3 4
    # 588 "/usr/include/sys/cdefs.h" 2 3 4
    # 653 "/usr/include/sys/cdefs.h" 3 4
    # 1 "/usr/include/sys/_posix_availability.h" 1 3 4
    # 654 "/usr/include/sys/cdefs.h" 2 3 4
    # 65 "/usr/include/stdio.h" 2 3 4
    # 1 "/usr/include/Availability.h" 1 3 4
    # 184 "/usr/include/Availability.h" 3 4
    # 1 "/usr/include/AvailabilityInternal.h" 1 3 4
    # 185 "/usr/include/Availability.h" 2 3 4
    # 66 "/usr/include/stdio.h" 2 3 4
    
    # 1 "/usr/include/_types.h" 1 3 4
    # 27 "/usr/include/_types.h" 3 4
    # 1 "/usr/include/sys/_types.h" 1 3 4
    # 33 "/usr/include/sys/_types.h" 3 4
    # 1 "/usr/include/machine/_types.h" 1 3 4
    # 32 "/usr/include/machine/_types.h" 3 4
    # 1 "/usr/include/i386/_types.h" 1 3 4
    # 37 "/usr/include/i386/_types.h" 3 4
    typedef signed char __int8_t;
    
    
    
    typedef unsigned char __uint8_t;
    typedef short __int16_t;
    typedef unsigned short __uint16_t;
    typedef int __int32_t;
    typedef unsigned int __uint32_t;
    typedef long long __int64_t;
    typedef unsigned long long __uint64_t;
    
    typedef long __darwin_intptr_t;
    typedef unsigned int __darwin_natural_t;
    # 70 "/usr/include/i386/_types.h" 3 4
    typedef int __darwin_ct_rune_t;
    
    
    
    
    
    typedef union {
     char __mbstate8[128];
     long long _mbstateL;
    } __mbstate_t;
    
    typedef __mbstate_t __darwin_mbstate_t;
    
    
    typedef long int __darwin_ptrdiff_t;
    
    
    
    
    
    
    
    typedef long unsigned int __darwin_size_t;
    
    
    
    
    
    typedef __builtin_va_list __darwin_va_list;
    
    
    
    
    
    typedef int __darwin_wchar_t;
    
    
    
    
    typedef __darwin_wchar_t __darwin_rune_t;
    
    
    typedef int __darwin_wint_t;
    
    
    
    
    typedef unsigned long __darwin_clock_t;
    typedef __uint32_t __darwin_socklen_t;
    typedef long __darwin_ssize_t;
    typedef long __darwin_time_t;
    # 33 "/usr/include/machine/_types.h" 2 3 4
    # 34 "/usr/include/sys/_types.h" 2 3 4
    # 55 "/usr/include/sys/_types.h" 3 4
    typedef __int64_t __darwin_blkcnt_t;
    typedef __int32_t __darwin_blksize_t;
    typedef __int32_t __darwin_dev_t;
    typedef unsigned int __darwin_fsblkcnt_t;
    typedef unsigned int __darwin_fsfilcnt_t;
    typedef __uint32_t __darwin_gid_t;
    typedef __uint32_t __darwin_id_t;
    typedef __uint64_t __darwin_ino64_t;
    
    typedef __darwin_ino64_t __darwin_ino_t;
    
    
    
    typedef __darwin_natural_t __darwin_mach_port_name_t;
    typedef __darwin_mach_port_name_t __darwin_mach_port_t;
    typedef __uint16_t __darwin_mode_t;
    typedef __int64_t __darwin_off_t;
    typedef __int32_t __darwin_pid_t;
    typedef __uint32_t __darwin_sigset_t;
    typedef __int32_t __darwin_suseconds_t;
    typedef __uint32_t __darwin_uid_t;
    typedef __uint32_t __darwin_useconds_t;
    typedef unsigned char __darwin_uuid_t[16];
    typedef char __darwin_uuid_string_t[37];
    
    
    # 1 "/usr/include/sys/_pthread/_pthread_types.h" 1 3 4
    # 57 "/usr/include/sys/_pthread/_pthread_types.h" 3 4
    struct __darwin_pthread_handler_rec {
     void (*__routine)(void *);
     void *__arg;
     struct __darwin_pthread_handler_rec *__next;
    };
    
    struct _opaque_pthread_attr_t {
     long __sig;
     char __opaque[56];
    };
    
    struct _opaque_pthread_cond_t {
     long __sig;
     char __opaque[40];
    };
    
    struct _opaque_pthread_condattr_t {
     long __sig;
     char __opaque[8];
    };
    
    struct _opaque_pthread_mutex_t {
     long __sig;
     char __opaque[56];
    };
    
    struct _opaque_pthread_mutexattr_t {
     long __sig;
     char __opaque[8];
    };
    
    struct _opaque_pthread_once_t {
     long __sig;
     char __opaque[8];
    };
    
    struct _opaque_pthread_rwlock_t {
     long __sig;
     char __opaque[192];
    };
    
    struct _opaque_pthread_rwlockattr_t {
     long __sig;
     char __opaque[16];
    };
    
    struct _opaque_pthread_t {
     long __sig;
     struct __darwin_pthread_handler_rec *__cleanup_stack;
     char __opaque[8176];
    };
    
    typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t;
    typedef struct _opaque_pthread_cond_t __darwin_pthread_cond_t;
    typedef struct _opaque_pthread_condattr_t __darwin_pthread_condattr_t;
    typedef unsigned long __darwin_pthread_key_t;
    typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t;
    typedef struct _opaque_pthread_mutexattr_t __darwin_pthread_mutexattr_t;
    typedef struct _opaque_pthread_once_t __darwin_pthread_once_t;
    typedef struct _opaque_pthread_rwlock_t __darwin_pthread_rwlock_t;
    typedef struct _opaque_pthread_rwlockattr_t __darwin_pthread_rwlockattr_t;
    typedef struct _opaque_pthread_t *__darwin_pthread_t;
    # 81 "/usr/include/sys/_types.h" 2 3 4
    # 28 "/usr/include/_types.h" 2 3 4
    # 39 "/usr/include/_types.h" 3 4
    typedef int __darwin_nl_item;
    typedef int __darwin_wctrans_t;
    
    typedef __uint32_t __darwin_wctype_t;
    # 68 "/usr/include/stdio.h" 2 3 4
    
    
    
    # 1 "/usr/include/sys/_types/_va_list.h" 1 3 4
    # 31 "/usr/include/sys/_types/_va_list.h" 3 4
    typedef __darwin_va_list va_list;
    # 72 "/usr/include/stdio.h" 2 3 4
    # 1 "/usr/include/sys/_types/_size_t.h" 1 3 4
    # 30 "/usr/include/sys/_types/_size_t.h" 3 4
    typedef __darwin_size_t size_t;
    # 73 "/usr/include/stdio.h" 2 3 4
    # 1 "/usr/include/sys/_types/_null.h" 1 3 4
    # 74 "/usr/include/stdio.h" 2 3 4
    
    # 1 "/usr/include/sys/stdio.h" 1 3 4
    # 39 "/usr/include/sys/stdio.h" 3 4
    int renameat(int, const char *, int, const char *) __attribute__((availability(macosx,introduced=10.10)));
    
    
    
    
    
    
    int renamex_np(const char *, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)));
    int renameatx_np(int, const char *, int, const char *, unsigned int) __attribute__((availability(macosx,introduced=10.12))) __attribute__((availability(ios,introduced=10.0))) __attribute__((availability(tvos,introduced=10.0))) __attribute__((availability(watchos,introduced=3.0)));
    # 76 "/usr/include/stdio.h" 2 3 4
    
    typedef __darwin_off_t fpos_t;
    # 88 "/usr/include/stdio.h" 3 4
    struct __sbuf {
     unsigned char *_base;
     int _size;
    };
    
    
    struct __sFILEX;
    # 122 "/usr/include/stdio.h" 3 4
    typedef struct __sFILE {
     unsigned char *_p;
     int _r;
     int _w;
     short _flags;
     short _file;
     struct __sbuf _bf;
     int _lbfsize;
    
    
     void *_cookie;
     int (* _Nullable _close)(void *);
     int (* _Nullable _read) (void *, char *, int);
     fpos_t (* _Nullable _seek) (void *, fpos_t, int);
     int (* _Nullable _write)(void *, const char *, int);
    
    
     struct __sbuf _ub;
     struct __sFILEX *_extra;
     int _ur;
    
    
     unsigned char _ubuf[3];
     unsigned char _nbuf[1];
    
    
     struct __sbuf _lb;
    
    
     int _blksize;
     fpos_t _offset;
    } FILE;
    
    
    extern FILE *__stdinp;
    extern FILE *__stdoutp;
    extern FILE *__stderrp;
    # 231 "/usr/include/stdio.h" 3 4
    void clearerr(FILE *);
    int fclose(FILE *);
    int feof(FILE *);
    int ferror(FILE *);
    int fflush(FILE *);
    int fgetc(FILE *);
    int fgetpos(FILE * restrict, fpos_t *);
    char *fgets(char * restrict, int, FILE *);
    
    
    
    FILE *fopen(const char * restrict __filename, const char * restrict __mode) __asm("_" "fopen" );
    
    int fprintf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
    int fputc(int, FILE *);
    int fputs(const char * restrict, FILE * restrict) __asm("_" "fputs" );
    size_t fread(void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream);
    FILE *freopen(const char * restrict, const char * restrict,
                     FILE * restrict) __asm("_" "freopen" );
    int fscanf(FILE * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3)));
    int fseek(FILE *, long, int);
    int fsetpos(FILE *, const fpos_t *);
    long ftell(FILE *);
    size_t fwrite(const void * restrict __ptr, size_t __size, size_t __nitems, FILE * restrict __stream) __asm("_" "fwrite" );
    int getc(FILE *);
    int getchar(void);
    char *gets(char *);
    void perror(const char *);
    int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
    int putc(int, FILE *);
    int putchar(int);
    int puts(const char *);
    int remove(const char *);
    int rename (const char *__old, const char *__new);
    void rewind(FILE *);
    int scanf(const char * restrict, ...) __attribute__((__format__ (__scanf__, 1, 2)));
    void setbuf(FILE * restrict, char * restrict);
    int setvbuf(FILE * restrict, char * restrict, int, size_t);
    int sprintf(char * restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((__availability__(swift, unavailable, message="Use snprintf instead.")));
    int sscanf(const char * restrict, const char * restrict, ...) __attribute__((__format__ (__scanf__, 2, 3)));
    FILE *tmpfile(void);
    
    __attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead.")))
    
    __attribute__((deprecated("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead.")))
    
    char *tmpnam(char *);
    int ungetc(int, FILE *);
    int vfprintf(FILE * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0)));
    int vprintf(const char * restrict, va_list) __attribute__((__format__ (__printf__, 1, 0)));
    int vsprintf(char * restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((__availability__(swift, unavailable, message="Use vsnprintf instead.")));
    # 297 "/usr/include/stdio.h" 3 4
    char *ctermid(char *);
    
    
    
    
    
    FILE *fdopen(int, const char *) __asm("_" "fdopen" );
    
    int fileno(FILE *);
    # 321 "/usr/include/stdio.h" 3 4
    int pclose(FILE *) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead.")));
    
    
    
    FILE *popen(const char *, const char *) __asm("_" "popen" ) __attribute__((__availability__(swift, unavailable, message="Use posix_spawn APIs or NSTask instead.")));
    # 342 "/usr/include/stdio.h" 3 4
    int __srget(FILE *);
    int __svfscanf(FILE *, const char *, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
    int __swbuf(int, FILE *);
    # 353 "/usr/include/stdio.h" 3 4
    inline __attribute__ ((__always_inline__)) int __sputc(int _c, FILE *_p) {
     if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
      return (*_p->_p++ = _c);
     else
      return (__swbuf(_c, _p));
    }
    # 379 "/usr/include/stdio.h" 3 4
    void flockfile(FILE *);
    int ftrylockfile(FILE *);
    void funlockfile(FILE *);
    int getc_unlocked(FILE *);
    int getchar_unlocked(void);
    int putc_unlocked(int, FILE *);
    int putchar_unlocked(int);
    
    
    
    int getw(FILE *);
    int putw(int, FILE *);
    
    
    __attribute__((__availability__(swift, unavailable, message="Use mkstemp(3) instead.")))
    
    __attribute__((deprecated("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of tempnam(3), it is highly recommended that you use mkstemp(3) instead.")))
    
    char *tempnam(const char *__dir, const char *__prefix) __asm("_" "tempnam" );
    # 417 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/sys/_types/_off_t.h" 1 3 4
    # 30 "/usr/include/sys/_types/_off_t.h" 3 4
    typedef __darwin_off_t off_t;
    # 418 "/usr/include/stdio.h" 2 3 4
    
    
    int fseeko(FILE * __stream, off_t __offset, int __whence);
    off_t ftello(FILE * __stream);
    
    
    
    
    
    int snprintf(char * restrict __str, size_t __size, const char * restrict __format, ...) __attribute__((__format__ (__printf__, 3, 4)));
    int vfscanf(FILE * restrict __stream, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
    int vscanf(const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 1, 0)));
    int vsnprintf(char * restrict __str, size_t __size, const char * restrict __format, va_list) __attribute__((__format__ (__printf__, 3, 0)));
    int vsscanf(const char * restrict __str, const char * restrict __format, va_list) __attribute__((__format__ (__scanf__, 2, 0)));
    # 442 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/sys/_types/_ssize_t.h" 1 3 4
    # 30 "/usr/include/sys/_types/_ssize_t.h" 3 4
    typedef __darwin_ssize_t ssize_t;
    # 443 "/usr/include/stdio.h" 2 3 4
    
    
    int dprintf(int, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3))) __attribute__((availability(macosx,introduced=10.7)));
    int vdprintf(int, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0))) __attribute__((availability(macosx,introduced=10.7)));
    ssize_t getdelim(char ** restrict __linep, size_t * restrict __linecapp, int __delimiter, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7)));
    ssize_t getline(char ** restrict __linep, size_t * restrict __linecapp, FILE * restrict __stream) __attribute__((availability(macosx,introduced=10.7)));
    # 458 "/usr/include/stdio.h" 3 4
    extern const int sys_nerr;
    extern const char *const sys_errlist[];
    
    int asprintf(char ** restrict, const char * restrict, ...) __attribute__((__format__ (__printf__, 2, 3)));
    char *ctermid_r(char *);
    char *fgetln(FILE *, size_t *);
    const char *fmtcheck(const char *, const char *);
    int fpurge(FILE *);
    void setbuffer(FILE *, char *, int);
    int setlinebuf(FILE *);
    int vasprintf(char ** restrict, const char * restrict, va_list) __attribute__((__format__ (__printf__, 2, 0)));
    FILE *zopen(const char *, const char *, int);
    
    
    
    
    
    FILE *funopen(const void *,
                     int (* _Nullable)(void *, char *, int),
                     int (* _Nullable)(void *, const char *, int),
                     fpos_t (* _Nullable)(void *, fpos_t, int),
                     int (* _Nullable)(void *));
    # 498 "/usr/include/stdio.h" 3 4
    # 1 "/usr/include/secure/_stdio.h" 1 3 4
    # 31 "/usr/include/secure/_stdio.h" 3 4
    # 1 "/usr/include/secure/_common.h" 1 3 4
    # 32 "/usr/include/secure/_stdio.h" 2 3 4
    # 42 "/usr/include/secure/_stdio.h" 3 4
    extern int __sprintf_chk (char * restrict, int, size_t,
         const char * restrict, ...);
    # 52 "/usr/include/secure/_stdio.h" 3 4
    extern int __snprintf_chk (char * restrict, size_t, int, size_t,
          const char * restrict, ...);
    
    
    
    
    
    
    
    extern int __vsprintf_chk (char * restrict, int, size_t,
          const char * restrict, va_list);
    
    
    
    
    
    
    
    extern int __vsnprintf_chk (char * restrict, size_t, int, size_t,
           const char * restrict, va_list);
    # 499 "/usr/include/stdio.h" 2 3 4
    # 2 "hello.c" 2
    
    int main()
    {
          printf("Hello! This is our embedded world!\n");
    
          return 0;
    }
    

    (2)编译阶段(最重要的阶段)

    可接收.c和.i类型的文件 ---> .s文件

    gcc -S hello.i -o hello.s
    gcc -S只进行编译(而不进行汇编,生成汇编代码)

    编译阶段gcc首先要检查代码的规范性、是否有语法错误等,以确定代码的实际要做的工作。

    检查无误后gcc把代码翻译成汇编语言 *.i编译成*.s文件

    hello.s 内容如下:

        .section    __TEXT,__text,regular,pure_instructions
        .macosx_version_min 10, 12
        .globl  _main
        .align  4, 0x90
    _main:                                  ## @main
        .cfi_startproc
    ## BB#0:
        pushq   %rbp
    Ltmp0:
        .cfi_def_cfa_offset 16
    Ltmp1:
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
    Ltmp2:
        .cfi_def_cfa_register %rbp
        subq    $16, %rsp
        leaq    L_.str(%rip), %rdi
        movl    $0, -4(%rbp)
        movb    $0, %al
        callq   _printf
        xorl    %ecx, %ecx
        movl    %eax, -8(%rbp)          ## 4-byte Spill
        movl    %ecx, %eax
        addq    $16, %rsp
        popq    %rbp
        retq
        .cfi_endproc
    
        .section    __TEXT,__cstring,cstring_literals
    L_.str:                                 ## @.str
        .asciz  "Hello! This is our embedded world!\n"
    
    
    .subsections_via_symbols
    

    (2)编译阶段(最重要的阶段)

    可接收.c和.i类型的文件 ---> .s文件

    gcc -S hello.i -o hello.s
    gcc -S只进行编译(而不进行汇编,生成汇编代码)

    编译阶段gcc首先要检查代码的规范性、是否有语法错误等,以确定代码的实际要做的工作。

    检查无误后gcc把代码翻译成汇编语言 *.i编译成*.s文件

    hello.s 内容如下:

        .section    __TEXT,__text,regular,pure_instructions
        .macosx_version_min 10, 12
        .globl  _main
        .align  4, 0x90
    _main:                                  ## @main
        .cfi_startproc
    ## BB#0:
        pushq   %rbp
    Ltmp0:
        .cfi_def_cfa_offset 16
    Ltmp1:
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
    Ltmp2:
        .cfi_def_cfa_register %rbp
        subq    $16, %rsp
        leaq    L_.str(%rip), %rdi
        movl    $0, -4(%rbp)
        movb    $0, %al
        callq   _printf
        xorl    %ecx, %ecx
        movl    %eax, -8(%rbp)          ## 4-byte Spill
        movl    %ecx, %eax
        addq    $16, %rsp
        popq    %rbp
        retq
        .cfi_endproc
    
        .section    __TEXT,__cstring,cstring_literals
    L_.str:                                 ## @.str
        .asciz  "Hello! This is our embedded world!\n"
    
    
    .subsections_via_symbols
    

    (3)汇编阶段

    汇编阶段可接收 .c .i .s 类型的文件 ---> .o文件

    把 .s文件 翻译成 二进制机器指令 .o文件:
    gcc -c hello.s -o hello.o

    -c告诉gcc进行汇编处理。
    这步生成的文件是二进制文件,用文本工具打开看到的将是“乱码”,使用反汇编工具(如GDB)才能读懂它。

    (4)链接阶段

    链接阶段。
    在这里涉及到一个重要的概念:函数库。

    链接的作用
    例子的源码中并没有定义”printf”的函数实现,且预编译中包含进的”stdio.h”中也只有该函数的声明,而没有定义函数的实现。
    那么哪里实现了”printf”函数?动态库文件libc.so.6
    系统把这些函数实现都做到名为libc.so.6的库文件中,在没有特别指定时,Gcc会到系统默认的搜索路径”/usr/lib”下进行查找,链接到libc.so.6库函数中就能实现函数”printf”

    函数库一般分为静态库和动态库两种。Gcc在编译时默认使用动态库

    静态库:指编译链接时,把库文件的代码全部加入到可执行文件中,运行时也就不再需要库文件,因此生成的文件比较大。
    后缀名一般为”.a”

    动态库:编译链接时并没有把库文件的代码加入到可执行文件中,而是在程序执行时由运行时链接文件加载库,节省系统的开销。
    动态库一般后缀名为”.so”
    如前面所述的libc.so.6就是动态库。

    生成静态库:
    ·ar cr libxxx.a file1.o file2.o
    把file1.o和file2.o打包 生成libxxx.a静态库

    生成动态库:
    gcc -fPIC -shared file1.c -o libtest.so
    也可以分成两部来写:
    gcc -fPIC file1.c -c //这一步生成file1.o
    gcc -shared file1.o -o libtest.so

    使用
    gcc test.c -L/path -libxxx -o test

    效果是一样的。
    使用动态库,运行程序时要指定动态库的位置,
    用环境变量来指定export LD_LIBRARY_PATH=path
    否则会提示找不到动态库的位置

    链接动态库/静态库 使用的方法是一样的,
    如果在库中有同名的静态库文件和动态库文件,如libtest.a和libtest.so
    gcc链接时默认优先选择动态库 即 链接libtest.so
    强制gcc链接静态库文件(如libtest.a):加选项-static

    静态库链接时搜索路径顺序:

    1. ld会去找GCC命令中的参数-L
    2. 再找gcc的环境变量LIBRARY_PATH
    3. 再找内定目录 /lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的
      动态链接时、执行时搜索路径顺序:
    4. 编译目标代码时指定的动态库搜索路径
    5. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径
    6. 配置文件/etc/ld.so.conf中指定的动态库搜索路径
    7. 默认的动态库搜索路径/lib
    8. 默认的动态库搜索路径/usr/lib

    有关环境变量:
    LIBRARY_PATH环境变量:指定程序静态链接库文件搜索路径
    LD_LIBRARY_PATH环境变量:指定程序动态链接库文件搜索路径

    完成了链接之后,Gcc就可以生成可执行文件,如下所示。
    gcc hello.o -o hello

    运行该可执行文件,出现正确的结果如下。
    ./hello

    Hello! This is our embedded world!

    选项(OPTIONS)

    选项必须分立给出: -dr 完全不同于-d -r

    相关文章

      网友评论

          本文标题:GCC 中文手册 (1)源代码->可执行文件

          本文链接:https://www.haomeiwen.com/subject/bjtgottx.html