新聞中心
redhat 6.5 gcc編譯器的知識(shí)點(diǎn)有哪些,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
成都創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站制作、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項(xiàng)目涵蓋了網(wǎng)頁設(shè)計(jì)、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、手機(jī)網(wǎng)站制作設(shè)計(jì)等網(wǎng)站方面業(yè)務(wù)。
測試結(jié)論
1,gcc是編譯器,分為4個(gè)階段
2,4個(gè)階段為
預(yù)處理階段
編譯階段
匯編階段
鏈接階段
3,預(yù)處理階段為 即把stdio.h頭文件內(nèi)容編譯進(jìn)來
4,編譯階段為GCC首先要檢查代碼的規(guī)范性,是否有語法錯(cuò)誤,以確定代碼實(shí)際要作的工作,在檢查無誤后,
GCC把代碼編譯成匯編代碼
5,GCC第3個(gè)階段即GCC的匯編階段,即把上述GCC第2個(gè)階段產(chǎn)生的.S文件轉(zhuǎn)化成目標(biāo)文件
6,GCC第4個(gè)階段,即進(jìn)入GCC的鏈接階段。在這里涉及一個(gè)重要的概念:函數(shù)庫
我們可以再次查看這個(gè)小程序即helloworld.c,在這個(gè)程序中并沒有定義printf函數(shù)的實(shí)現(xiàn),
且在其預(yù)編譯包含進(jìn)來的頭文件stdio.h中也只有這個(gè)函數(shù)的聲明,而沒有這個(gè)函數(shù)的實(shí)現(xiàn),那么到底在哪兒實(shí)現(xiàn)
printf函數(shù)的呢。答案是:操作系統(tǒng)會(huì)把這些函數(shù)實(shí)現(xiàn)都作到一個(gè)名為libc.so.6的庫文件中去了,如果在沒有特別指定時(shí),
GCC會(huì)到操作系統(tǒng)默認(rèn)的路徑即/USR/LIB查找,也就是說會(huì)去鏈接到這個(gè)路徑的LIBC.SO.6庫函數(shù)中,這樣最終就可以實(shí)現(xiàn)函數(shù)PRINTF的功能了
7,函數(shù)庫一般分2處類型:
靜態(tài)庫和動(dòng)態(tài)庫
靜態(tài)庫是指在編譯鏈接時(shí),把庫文件的代碼會(huì)全部加入到可執(zhí)行文件中,因此生成的文件會(huì)比較大,但在運(yùn)行時(shí)也就不需要庫文件了,其后綴一般為.a,
動(dòng)態(tài)庫,與靜態(tài)庫相反,在編譯鏈接時(shí),并沒有把庫文件的代碼加入到可執(zhí)行文件中,而是在程序執(zhí)行時(shí)鏈接文件加載庫,這樣就可以節(jié)省系統(tǒng)的開銷,
動(dòng)態(tài)庫一般后綴為.SO,如前述LIBC.SO.6就是動(dòng)態(tài)庫。
GCC在編譯時(shí)默認(rèn)采用動(dòng)態(tài)庫
測試明細(xì)
1,操作系統(tǒng)版本
[root@mygirl ~]# more /etc/redhat-release
Red Hat Enterprise Linux Server release 6.5 (Santiago)
[root@mygirl ~]#
2,編寫一個(gè)helloworld.c源代碼文件
[root@mygirl ~]# pwd
/root
[root@mygirl ~]#
clude
int main() {
printf("hello world!\n");
return 0;
}
[root@mygirl ~]#
3,進(jìn)行g(shù)cc 4個(gè)階段第一個(gè)階段 預(yù)處理階段,即把stdio.h頭文件內(nèi)容編譯進(jìn)來
[root@mygirl ~]# gcc -E helloworld.c -o helloworld.i
[root@mygirl ~]#
[root@mygirl ~]# ll hel*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
4,查看GCC預(yù)處理階段產(chǎn)生的文件,可見此文件內(nèi)容極多
[root@mygirl ~]# more helloworld.i|more
# 1 "helloworld.c"
# 1 ""
# 1 ""
# 1 "helloworld.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 28 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 361 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 365 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 366 "/usr/include/sys/cdefs.h" 2 3 4
# 362 "/usr/include/features.h" 2 3 4
# 385 "/usr/include/features.h" 3 4
# 1 "/usr/include/gnu/stubs.h" 1 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 5 "/usr/include/gnu/stubs.h" 2 3 4
# 1 "/usr/include/gnu/stubs-64.h" 1 3 4
# 10 "/usr/include/gnu/stubs.h" 2 3 4
# 386 "/usr/include/features.h" 2 3 4
# 29 "/usr/include/stdio.h" 2 3 4
5,經(jīng)查stdio.h頭文件是把其內(nèi)容通過GCC預(yù)處理階段添加到其預(yù)處理的產(chǎn)生的文件中
[root@mygirl ~]# locate stdio.h
/oracle/product/11.2.0/db_1/perl/lib/5.10.0/x86_64-linux-thread-multi/CORE/nostdio.h
/usr/include/stdio.h
/usr/include/bits/stdio.h
[root@mygirl ~]# more /usr/include/stdio.h|more
/* Define ISO C stdio on top of C++ iostreams.
Copyright (C) 1991, 1994-2008, 2009, 2010 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
/*
* ISO C99 Standard: 7.19 Input/output
*/
#ifndef _STDIO_H
#if !defined __need_FILE && !defined __need___FILE
# define _STDIO_H 1
# include
6,進(jìn)行g(shù)cc第二階段之編譯階段,這個(gè)階段就是GCC首先要檢查代碼的規(guī)范性,是否有語法錯(cuò)誤,以確定代碼實(shí)際要作的工作,在檢查無誤后,
GCC把代碼編譯成匯編代碼
[root@mygirl ~]# ll helloworld.*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
[root@mygirl ~]# gcc -S helloworld.i -o helloworld.s
[root@mygirl ~]#
[root@mygirl ~]# ll helloworld.*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
-rw-r--r--. 1 root root 448 Jun 30 06:38 helloworld.s
[root@mygirl ~]# more helloworld.s
.file "helloworld.c"
.section .rodata
.LC0:
.string "hello world!"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $.LC0, %edi
call puts
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
.section .note.GNU-stack,"",@progbits
7,進(jìn)行GCC第3個(gè)階段即GCC的匯編階段,即把上述GCC第2個(gè)階段產(chǎn)生的.S文件轉(zhuǎn)化成目標(biāo)文件
/root
[root@mygirl ~]# ll helloworld.*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
-rw-r--r--. 1 root root 448 Jun 30 06:38 helloworld.s
[root@mygirl ~]#
[root@mygirl ~]#
8,進(jìn)行GCC第4個(gè)階段,即進(jìn)入GCC的鏈接階段。在這里涉及一個(gè)重要的概念:函數(shù)庫
我們可以再次查看這個(gè)小程序即helloworld.c,在這個(gè)程序中并沒有定義printf函數(shù)的實(shí)現(xiàn),
且在其預(yù)編譯包含進(jìn)來的頭文件stdio.h中也只有這個(gè)函數(shù)的聲明,而沒有這個(gè)函數(shù)的實(shí)現(xiàn),那么到底在哪兒實(shí)現(xiàn)
printf函數(shù)的呢。答案是:操作系統(tǒng)會(huì)把這些函數(shù)實(shí)現(xiàn)都作到一個(gè)名為libc.so.6的庫文件中去了,如果在沒有特別指定時(shí),
GCC會(huì)到操作系統(tǒng)默認(rèn)的路徑即/USR/LIB查找,也就是說會(huì)去鏈接到這個(gè)路徑的LIBC.SO.6庫函數(shù)中,這樣最終就可以實(shí)現(xiàn)函數(shù)PRINTF的功能了
[root@mygirl ~]# gcc -c helloworld.s -o helloworld.o
[root@mygirl ~]#
[root@mygirl ~]#
[root@mygirl ~]# ll helloworld.*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
-rw-r--r--. 1 root root 1504 Jun 30 06:46 helloworld.o
-rw-r--r--. 1 root root 448 Jun 30 06:38 helloworld.s
[root@mygirl ~]# strings helloworld.o
hello world!
[root@mygirl ~]#
9,
函數(shù)庫一般分2處類型:
靜態(tài)庫和動(dòng)態(tài)庫
靜態(tài)庫是指在編譯鏈接時(shí),把庫文件的代碼會(huì)全部加入到可執(zhí)行文件中,因此生成的文件會(huì)比較大,但在運(yùn)行時(shí)也就不需要庫文件了,其后綴一般為.a,
動(dòng)態(tài)庫,與靜態(tài)庫相反,在編譯鏈接時(shí),并沒有把庫文件的代碼加入到可執(zhí)行文件中,而是在程序執(zhí)行時(shí)鏈接文件加載庫,這樣就可以節(jié)省系統(tǒng)的開銷,
動(dòng)態(tài)庫一般后綴為.SO,如前述LIBC.SO.6就是動(dòng)態(tài)庫。
GCC在編譯時(shí)默認(rèn)采用動(dòng)態(tài)庫
[root@mygirl ~]# ll helloworld.*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
-rw-r--r--. 1 root root 1504 Jun 30 06:46 helloworld.o
-rw-r--r--. 1 root root 448 Jun 30 06:38 helloworld.s
[root@mygirl ~]#
[root@mygirl ~]# gcc helloworld.o -o helloworld
[root@mygirl ~]#
[root@mygirl ~]#
[root@mygirl ~]# ll helloworld.*
-rw-r--r--. 1 root root 76 Jun 30 06:20 helloworld.c
-rw-r--r--. 1 root root 16751 Jun 30 06:22 helloworld.i
-rw-r--r--. 1 root root 1504 Jun 30 06:46 helloworld.o
-rw-r--r--. 1 root root 448 Jun 30 06:38 helloworld.s
[root@mygirl ~]#
[root@mygirl ~]# ll helloworld
-rwxr-xr-x. 1 root root 6430 Jun 30 06:58 helloworld
[root@mygirl ~]#
10,查看GCC第4個(gè)階段即GCC的鏈接階段產(chǎn)生不代擴(kuò)展名的文件名
[root@mygirl ~]# strings helloworld
/lib64/ld-linux-x86-64.so.2
__gmon_start__
libc.so.6
puts
__libc_start_main
GLIBC_2.2.5
fff.
fffff.
l$ L
t$(L
|$0H
hello world!
[root@mygirl ~]#
11,執(zhí)行GCC第4個(gè)階段產(chǎn)生的執(zhí)行文件
[root@mygirl ~]# ./helloworld
hello world!
[root@mygirl ~]#
12,查看動(dòng)態(tài)庫文件
[root@mygirl ~]# locate libc.so.6
/lib/libc.so.6
/lib/i686/nosegneg/libc.so.6
/lib64/libc.so.6
/oracle/product/11.2.0/db_1/lib/stubs/libc.so.6
[root@mygirl ~]# strings /lib/libc.so.6 |grep -i --color printf
vswprintf
__obstack_vprintf_chk
vasprintf
printf_size
__vfprintf_chk
vfwprintf
__vsprintf_chk
[root@mygirl ~]# more /oracle/product/11.2.0/db_1/lib/sysliblist
-ldl -lm -lpthread -lnsl -lirc -lipgo -lsvml
看完上述內(nèi)容,你們掌握redhat 6.5 gcc編譯器的知識(shí)點(diǎn)有哪些的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前名稱:redhat6.5gcc編譯器的知識(shí)點(diǎn)有哪些
文章出自:http://ef60e0e.cn/article/jojepd.html