GCC 中文手冊(cè) (1)源代碼->可執(zhí)行文件

本手冊(cè)頁內(nèi)容摘自 GNU C 編譯器的完整文檔,僅限于解釋選項(xiàng)的含義.
如果發(fā)現(xiàn)手冊(cè)頁和Info 文件之間有所矛盾,請(qǐng)查對(duì) Info 文件(權(quán)威文檔)

GCC

gcc GNU 工程的 C 編譯器
g++ GNU 工程的 C++編譯器

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

描述(DESCRIPTION)

C 和 C++編譯器是集成的
都要用4個(gè)步驟中的一個(gè)或多個(gè)處理輸入文件:
預(yù)處理 (preprocessing)      cpp -Egcc -E
編譯(compilation)        cc1 -Sgcc -S
匯編(assembly)          as
鏈接(linking)          ld

源文件后綴名標(biāo)識(shí)源文件的語言,但是對(duì)編譯器來說,后綴名控制著缺省設(shè)置:
  • gcc 認(rèn)為預(yù)處理后的文件(.i)是 C 文件,并且設(shè)定 C 形式的連接.

  • g++ 認(rèn)為預(yù)處理后的文件(.i)是 C++文件,并且設(shè)定 C++形式的連接.

源文件后綴名指出語言種類以及后期的操作:

.c       C源程序        預(yù)處理敷硅,編譯畔濒,匯編
.C      C++源程序       預(yù)處理洗鸵,編譯康二,匯編
.cc      C++源程序       預(yù)處理贱纠,編譯,匯編
.cxx      C++源程序       預(yù)處理,編譯,匯編
.m       Objective-C源程序   預(yù)處理犀变,編譯,匯編
.i       預(yù)處理后的C文件     編譯秋柄,匯編
.ii      預(yù)處理后的C++文件   編譯获枝,匯編
.s      匯編語言源程序     匯編
.S      匯編語言源程序     預(yù)處理,匯編
.h      預(yù)處理器文件      通常不出現(xiàn)在命令行上

其他后綴名的文件被傳遞給連接器(linker).通常包括:

.o     目標(biāo)文件(Object file)
.a     歸檔庫文件(Archive file)

除非使用了-c, -S,或-E 選項(xiàng)(或者編譯錯(cuò)誤阻止了完整的過程),否則連接總是最后的步驟.
在連接階段中,所有對(duì)應(yīng)于源程序的.o 文件, -l 庫文件,無法識(shí)別的文件名(包括指定的 .o 目 標(biāo)文件和.a 庫文件)按命令行中的順序傳遞給連接器.

例子

hello.c源代碼:

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

(1)預(yù)處理階段
預(yù)處理階段 主要處理#include和#define

  • #include編譯器把#include.h文件 包含到源代碼中
  • #define定義的 源代碼中實(shí)際使用到的 宏 用實(shí)際的字符串代替

預(yù)處理階段 可理解為簡單的文本相加/替換:
hello.c + .h文件源碼 替換宏定義define ---> hello.i

gcc命令的一般格式

gcc [選項(xiàng)] 要編譯的文件 [選項(xiàng)] [目標(biāo)文件]
( -o 參數(shù)省略時(shí)骇笔,Gcc默認(rèn)生成可執(zhí)行的文件:編譯文件.out )

例子
Gcc –E hello.c –o hello.i
gcc -E 表示只進(jìn)行預(yù)處理(不進(jìn)行后面的3個(gè)階段)
-o 指目標(biāo)文件(gcc輸出的文件)
.i 文件為已經(jīng)過預(yù)處理的C原始程序 hello.i的內(nè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只進(jìn)行編譯(而不進(jìn)行匯編省店,生成匯編代碼)

編譯階段gcc首先要檢查代碼的規(guī)范性嚣崭、是否有語法錯(cuò)誤等,以確定代碼的實(shí)際要做的工作懦傍。

檢查無誤后gcc把代碼翻譯成匯編語言 *.i編譯成*.s文件

hello.s 內(nèi)容如下:

    .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只進(jìn)行編譯(而不進(jìn)行匯編雹舀,生成匯編代碼)

編譯階段gcc首先要檢查代碼的規(guī)范性、是否有語法錯(cuò)誤等谎脯,以確定代碼的實(shí)際要做的工作葱跋。

檢查無誤后gcc把代碼翻譯成匯編語言 *.i編譯成*.s文件

hello.s 內(nèi)容如下:

    .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文件 翻譯成 二進(jìn)制機(jī)器指令 .o文件:
gcc -c hello.s -o hello.o

-c告訴gcc進(jìn)行匯編處理持寄。
這步生成的文件是二進(jìn)制文件源梭,用文本工具打開看到的將是“亂碼”,使用反匯編工具(如GDB)才能讀懂它稍味。

(4)鏈接階段

鏈接階段废麻。
在這里涉及到一個(gè)重要的概念:函數(shù)庫。

鏈接的作用
例子的源碼中并沒有定義”printf”的函數(shù)實(shí)現(xiàn)模庐,且預(yù)編譯中包含進(jìn)的”stdio.h”中也只有該函數(shù)的聲明烛愧,而沒有定義函數(shù)的實(shí)現(xiàn)。
那么哪里實(shí)現(xiàn)了”printf”函數(shù)掂碱?動(dòng)態(tài)庫文件libc.so.6
系統(tǒng)把這些函數(shù)實(shí)現(xiàn)都做到名為libc.so.6的庫文件中怜姿,在沒有特別指定時(shí),Gcc會(huì)到系統(tǒng)默認(rèn)的搜索路徑”/usr/lib”下進(jìn)行查找疼燥,鏈接到libc.so.6庫函數(shù)中就能實(shí)現(xiàn)函數(shù)”printf”

函數(shù)庫一般分為靜態(tài)庫和動(dòng)態(tài)庫兩種沧卢。Gcc在編譯時(shí)默認(rèn)使用動(dòng)態(tài)庫

靜態(tài)庫:指編譯鏈接時(shí),把庫文件的代碼全部加入到可執(zhí)行文件中醉者,運(yùn)行時(shí)也就不再需要庫文件但狭,因此生成的文件比較大。
后綴名一般為”.a”

動(dòng)態(tài)庫:編譯鏈接時(shí)并沒有把庫文件的代碼加入到可執(zhí)行文件中撬即,而是在程序執(zhí)行時(shí)由運(yùn)行時(shí)鏈接文件加載庫立磁,節(jié)省系統(tǒng)的開銷。
動(dòng)態(tài)庫一般后綴名為”.so”
如前面所述的libc.so.6就是動(dòng)態(tài)庫剥槐。

生成靜態(tài)庫:
·ar cr libxxx.a file1.o file2.o
把file1.o和file2.o打包 生成libxxx.a靜態(tài)庫

生成動(dòng)態(tài)庫:
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

效果是一樣的唱歧。
使用動(dòng)態(tài)庫,運(yùn)行程序時(shí)要指定動(dòng)態(tài)庫的位置粒竖,
用環(huán)境變量來指定export LD_LIBRARY_PATH=path
否則會(huì)提示找不到動(dòng)態(tài)庫的位置

鏈接動(dòng)態(tài)庫/靜態(tài)庫 使用的方法是一樣的颅崩,
如果在庫中有同名的靜態(tài)庫文件和動(dòng)態(tài)庫文件,如libtest.a和libtest.so
gcc鏈接時(shí)默認(rèn)優(yōu)先選擇動(dòng)態(tài)庫 即 鏈接libtest.so
強(qiáng)制gcc鏈接靜態(tài)庫文件(如libtest.a):加選項(xiàng)-static

靜態(tài)庫鏈接時(shí)搜索路徑順序:

  1. ld會(huì)去找GCC命令中的參數(shù)-L
  2. 再找gcc的環(huán)境變量LIBRARY_PATH
  3. 再找內(nèi)定目錄 /lib /usr/lib /usr/local/lib 這是當(dāng)初compile gcc時(shí)寫在程序內(nèi)的
    動(dòng)態(tài)鏈接時(shí)温圆、執(zhí)行時(shí)搜索路徑順序:
  4. 編譯目標(biāo)代碼時(shí)指定的動(dòng)態(tài)庫搜索路徑
  5. 環(huán)境變量LD_LIBRARY_PATH指定的動(dòng)態(tài)庫搜索路徑
  6. 配置文件/etc/ld.so.conf中指定的動(dòng)態(tài)庫搜索路徑
  7. 默認(rèn)的動(dòng)態(tài)庫搜索路徑/lib
  8. 默認(rèn)的動(dòng)態(tài)庫搜索路徑/usr/lib

有關(guān)環(huán)境變量:
LIBRARY_PATH環(huán)境變量:指定程序靜態(tài)鏈接庫文件搜索路徑
LD_LIBRARY_PATH環(huán)境變量:指定程序動(dòng)態(tài)鏈接庫文件搜索路徑

完成了鏈接之后挨摸,Gcc就可以生成可執(zhí)行文件,如下所示岁歉。
gcc hello.o -o hello

運(yùn)行該可執(zhí)行文件得运,出現(xiàn)正確的結(jié)果如下膝蜈。
./hello

Hello! This is our embedded world!

選項(xiàng)(OPTIONS)

選項(xiàng)必須分立給出: -dr 完全不同于-d -r

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市熔掺,隨后出現(xiàn)的幾起案子饱搏,更是在濱河造成了極大的恐慌,老刑警劉巖置逻,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件推沸,死亡現(xiàn)場離奇詭異,居然都是意外死亡券坞,警方通過查閱死者的電腦和手機(jī)鬓催,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恨锚,“玉大人宇驾,你說我怎么就攤上這事『锪妫” “怎么了课舍?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長他挎。 經(jīng)常有香客問我筝尾,道長,這世上最難降的妖魔是什么办桨? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任筹淫,我火速辦了婚禮,結(jié)果婚禮上崔挖,老公的妹妹穿的比我還像新娘贸街。我一直安慰自己,他們只是感情好狸相,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布薛匪。 她就那樣靜靜地躺著,像睡著了一般脓鹃。 火紅的嫁衣襯著肌膚如雪逸尖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天瘸右,我揣著相機(jī)與錄音娇跟,去河邊找鬼。 笑死太颤,一個(gè)胖子當(dāng)著我的面吹牛苞俘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播龄章,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼吃谣,長吁一口氣:“原來是場噩夢啊……” “哼乞封!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起岗憋,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤肃晚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后仔戈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體关串,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年监徘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了晋修。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡耐量,死狀恐怖飞蚓,靈堂內(nèi)的尸體忽然破棺而出滤港,到底是詐尸還是另有隱情廊蜒,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布溅漾,位于F島的核電站山叮,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏添履。R本人自食惡果不足惜屁倔,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望暮胧。 院中可真熱鬧锐借,春花似錦、人聲如沸往衷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽席舍。三九已至布轿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間来颤,已是汗流浹背汰扭。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留福铅,地道東北人萝毛。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像滑黔,于是被迫代替她去往敵國和親笆包。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鲁冯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容