unixc編譯器怎麼查看版本
1. linux查看版本命令問題
Linux下查看版本號的命令
1,查看內核版本命令:
cat /proc/version
uname -a
uname -rcat /etc/issue
man uname
2,查看linux版本:抄錄如下:
1) 登錄到伺服器執行 lsb_release -a ,即可列出所有版本信息,例如:
[[email protected] ~]# lsb_release -a
LSB Version: 1.3
Distributor ID: RedHatEnterpriseAS
Descrīption: Red Hat Enterprise Linux AS release 4 (Nahant Update 1)
Release: 4
Codename: NahantUpdate1
[[email protected] ~]#
這個命令適用於所有的linux,包括Redhat、SuSE、Debian等發行版。
2) 登錄到linux執行cat /etc/redhat-release ,例如如下:
[[email protected] ~]# cat /etc/redhat-release
Red Hat Enterprise Linux AS release 4 (Nahant Update 1)
[[email protected] ~]#
這種方式下可以直接看到具體的版本號,比如 AS4 Update 1
3)登錄到linux執行rpm -q redhat-release ,例如如下
[[email protected] ~]# rpm -q redhat-release
redhat-release-4AS-2.4
[[email protected] ~]#
這種方式下可看到一個所謂的release號,比如上邊的例子是2.4
這個release號和實際的版本之間存在一定的對應關系,如下:
redhat-release-3AS-1 -> Redhat Enterprise Linux AS 3
redhat-release-3AS-7.4 -> Redhat Enterprise Linux AS 3 Update 4
redhat-release-4AS-2 -> Redhat Enterprise Linux AS 4
redhat-release-4AS-2.4 -> Redhat Enterprise Linux AS 4 Update 1
redhat-release-4AS-3 -> Redhat Enterprise Linux AS 4 Update 2
redhat-release-4AS-4.1 -> Redhat Enterprise Linux AS 4 Update 3
redhat-release-4AS-5.5 -> Redhat Enterprise Linux AS 4 Update 4
更多Linux知識可參考書籍《Linux就該這么學》。
2. Linux中gcc編譯器如何使用
2004年4月20日最新版本的GCC編譯器3.4.0發布了。目前,GCC可以用來編譯C/C++、FORTRAN、java、OBJC、ADA等語言的程序,可根據需要選擇安裝支持的語言。GCC 3.4.0比以前版本更好地支持了C++標准。本文以在Redhat Linux上安裝GCC3.4.0為例,介紹了GCC的安裝過程。
安裝之前,系統中必須要有cc或者gcc等編譯器,並且是可用的,或者用環境變數CC指定系統上的編譯器。如果系統上沒有編譯器,不能安裝源代碼形式的GCC 3.4.0。如果是這種情況,可以在網上找一個與你系統相適應的如RPM等二進制形式的GCC軟體包來安裝使用。本文介紹的是以源代碼形式提供的GCC軟體包的安裝過程,軟體包本身和其安裝過程同樣適用於其它Linux和Unix系統。
系統上原來的GCC編譯器可能是把gcc等命令文件、庫文件、頭文件等分別存放到系統中的不同目錄下的。與此不同,現在GCC建議我們將一個版本的GCC安裝在一個單獨的目錄下。這樣做的好處是將來不需要它的時候可以方便地刪除整個目錄即可(因為GCC沒有uninstall功能);缺點是在安裝完成後要做一些設置工作才能使編譯器工作正常。在本文中我採用這個方案安裝GCC 3.4.0,並且在安裝完成後,仍然能夠使用原來低版本的GCC編譯器,即一個系統上可以同時存在並使用多個版本的GCC編譯器。
按照本文提供的步驟和設置選項,即使以前沒有安裝過GCC,也可以在系統上安裝上一個可工作的新版本的GCC編譯器。
1. 下載
在GCC網站上()或者通過網上搜索可以查找到下載資源。目前GCC的最新版本為 3.4.0。可供下載的文件一般有兩種形式:gcc-3.4.0.tar.gz和gcc-3.4.0.tar.bz2,只是壓縮格式不一樣,內容完全一致,下載其中一種即可。
2. 解壓縮
根據壓縮格式,選擇下面相應的一種方式解包(以下的「%」表示命令行提示符):
% tar xzvf gcc-3.4.0.tar.gz
或者
% bzcat gcc-3.4.0.tar.bz2 | tar xvf -
新生成的gcc-3.4.0這個目錄被稱為源目錄,用${srcdir}表示它。以後在出現${srcdir}的地方,應該用真實的路徑來替換它。用pwd命令可以查看當前路徑。
在${srcdir}/INSTALL目錄下有詳細的GCC安裝說明,可用瀏覽器打開index.html閱讀。
3. 建立目標目錄
目標目錄(用${objdir}表示)是用來存放編譯結果的地方。GCC建議編譯後的文件不要放在源目錄${srcdir]中(雖然這樣做也可以),最好單獨存放在另外一個目錄中,而且不能是${srcdir}的子目錄。
例如,可以這樣建立一個叫 gcc-build 的目標目錄(與源目錄${srcdir}是同級目錄):
% mkdir gcc-build
% cd gcc-build
以下的操作主要是在目標目錄 ${objdir} 下進行。
4. 配置
配置的目的是決定將GCC編譯器安裝到什麼地方(${destdir}),支持什麼語言以及指定其它一些選項等。其中,${destdir}不能與${objdir}或${srcdir}目錄相同。
配置是通過執行${srcdir}下的configure來完成的。其命令格式為(記得用你的真實路徑替換${destdir}):
% ${srcdir}/configure --prefix=${destdir} [其它選項]
例如,如果想將GCC 3.4.0安裝到/usr/local/gcc-3.4.0目錄下,則${destdir}就表示這個路徑。
在我的機器上,我是這樣配置的:
% ../gcc-3.4.0/configure --prefix=/usr/local/gcc-3.4.0 --enable-threads=posix --disable-checking --enable--long-long --host=i386-redhat-linux --with-system-zlib --enable-languages=c,c++,java
將GCC安裝在/usr/local/gcc-3.4.0目錄下,支持C/C++和JAVA語言,其它選項參見GCC提供的幫助說明。
5. 編譯
% make
這是一個漫長的過程。在我的機器上(P4-1.6),這個過程用了50多分鍾。
6. 安裝
執行下面的命令將編譯好的庫文件等拷貝到${destdir}目錄中(根據你設定的路徑,可能需要管理員的許可權):
% make install
至此,GCC 3.4.0安裝過程就完成了。
6. 其它設置
GCC 3.4.0的所有文件,包括命令文件(如gcc、g++)、庫文件等都在${destdir}目錄下分別存放,如命令文件放在bin目錄下、庫文件在lib下、頭文件在include下等。由於命令文件和庫文件所在的目錄還沒有包含在相應的搜索路徑內,所以必須要作適當的設置之後編譯器才能順利地找到並使用它們。
6.1 gcc、g++、gcj的設置
要想使用GCC 3.4.0的gcc等命令,簡單的方法就是把它的路徑${destdir}/bin放在環境變數PATH中。我不用這種方式,而是用符號連接的方式實現,這樣做的好處是我仍然可以使用系統上原來的舊版本的GCC編譯器。
首先,查看原來的gcc所在的路徑:
% which gcc
在我的系統上,上述命令顯示:/usr/bin/gcc。因此,原來的gcc命令在/usr/bin目錄下。我們可以把GCC 3.4.0中的gcc、g++、gcj等命令在/usr/bin目錄下分別做一個符號連接:
% cd /usr/bin
% ln -s ${destdir}/bin/gcc gcc34
% ln -s ${destdir}/bin/g++ g++34
% ln -s ${destdir}/bin/gcj gcj34
這樣,就可以分別使用gcc34、g++34、gcj34來調用GCC 3.4.0的gcc、g++、gcj完成對C、C++、JAVA程序的編譯了。同時,仍然能夠使用舊版本的GCC編譯器中的gcc、g++等命令。
6.2 庫路徑的設置
將${destdir}/lib路徑添加到環境變數LD_LIBRARY_PATH中,最好添加到系統的配置文件中,這樣就不必要每次都設置這個環境變數了。
例如,如果GCC 3.4.0安裝在/usr/local/gcc-3.4.0目錄下,在RH Linux下可以直接在命令行上執行或者在文件/etc/profile中添加下面一句:
setenv LD_LIBRARY_PATH /usr/local/gcc-3.4.0/lib:$LD_LIBRARY_PATH
7. 測試
用新的編譯命令(gcc34、g++34等)編譯你以前的C、C++程序,檢驗新安裝的GCC編譯器是否能正常工作。
8. 根據需要,可以刪除或者保留${srcdir}和${objdir}目錄。
3. 如何查看程序被哪個版本編譯器編譯的linux-gcc
那是不可能的,除非你加入了調試信息,也就是編譯的時候加入了-g參數,然後用gdb調試就可以顯示。最大程度上查看一個elf文件信息。
{
readelf -Wa a.out | head
readelf -wi a.out
readelf -p .comment a.out
objmp -s --section .comment audioplayer
}
如下:
[root@localhost rootfs]# readelf -Wa bin/gzip
復制代碼
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: ARM
Version: 0x1
Entry point address: 0xa080
Start of program headers: 52 (bytes into file)
Start of section headers: 1975444 (bytes into file)
Flags: 0x5000002, has entry point, Version5 EABI
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 6
Size of section headers: 40 (bytes)
Number of section headers: 25
Section header string table index: 24
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .note.ABI-tag NOTE 000080f4 0000f4 000020 00 A 0 0 4
[ 2] .init PROGBITS 00008114 000114 00000c 00 AX 0 0 4
[ 3] .text PROGBITS 00008120 000120 17fcfc 00 AX 0 0 16
[ 4] __libc_freeres_fn PROGBITS 00187e1c 17fe1c 000f20 00 AX 0 0 4
[ 5] __libc_thread_fre PROGBITS 00188d3c 180d3c 0000e4 00 AX 0 0 4
[ 6] .fini PROGBITS 00188e20 180e20 000008 00 AX 0 0 4
[ 7] .rodata PROGBITS 00188e28 180e28 058147 00 A 0 0 8
[ 8] __libc_subfreeres PROGBITS 001e0f70 1d8f70 00005c 00 A 0 0 4
[ 9] __libc_atexit PROGBITS 001e0fcc 1d8fcc 000004 00 A 0 0 4
[10] __libc_thread_sub PROGBITS 001e0fd0 1d8fd0 000008 00 A 0 0 4
[11] .ARM.extab PROGBITS 001e0fd8 1d8fd8 001b04 00 A 0 0 4
[12] .ARM.exidx ARM_EXIDX 001e2adc 1daadc 006ea8 00 AL 3 0 4
[13] .tdata PROGBITS 001f1984 1e1984 000018 00 WAT 0 0 4
[14] .tbss NOBITS 001f199c 1e199c 000034 00 WAT 0 0 4
[15] .init_array INIT_ARRAY 001f199c 1e199c 000004 00 WA 0 0 4
[16] .fini_array FINI_ARRAY 001f19a0 1e19a0 000008 00 WA 0 0 4
[17] .jcr PROGBITS 001f19a8 1e19a8 000004 00 WA 0 0 4
[18] .data.rel.ro PROGBITS 001f19ac 1e19ac 00002c 00 WA 0 0 4
[19] .got PROGBITS 001f19d8 1e19d8 00007c 04 WA 0 0 4
[20] .data PROGBITS 001f1a58 1e1a58 0008f7 00 WA 0 0 8
[21] .bss NOBITS 001f2350 1e234f 004828 00 WA 0 0 8
[22] __libc_freeres_pt NOBITS 001f6b78 1e234f 00003c 00 WA 0 0 4
[23] .ARM.attributes ARM_ATTRIBUTES 00000000 1e234f 00002b 00 0 0 1
[24] .shstrtab STRTAB 00000000 1e237a 000118 00 0 0 1
Section to Segment mapping:
Segment Sections...
00 .ARM.exidx
01 .note.ABI-tag .init .text __libc_freeres_fn __libc_thread_freeres_fn .fini .rodata __libc_subfreeres __libc_atexit __libc_thread_subfreeres .ARM.extab .ARM.exidx
02 .tdata .init_array .fini_array .jcr .data.rel.ro .got .data .bss __libc_freeres_ptrs
03 .note.ABI-tag
04 .tdata .tbss
Attribute Section: aeabi
File Attributes
Tag_CPU_name: "5TE"
Tag_CPU_arch: v5TE
Tag_ARM_ISA_use: Yes
Tag_THUMB_ISA_use: Thumb-1
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align8_needed: Yes
Tag_ABI_align8_preserved: Yes, except leaf SP
Tag_ABI_enum_size: int
Tag_unknown_44: 1 (0x1)
復制代碼
How to retrieve the GCC version used to compile a given ELF executable? http://stackoverflow.com/questions/2387040/how-to-retrieve-the-gcc-version-used-to-compile-a-given-elf-executable
QUES: I'd like to retrieve the GCC version used to compile a given executable. I tried readelf but didn't get the information. Any thoughts?
ANS: To complete what others have said: it's not stored in the object (or exe) file, unless you compile with debugging information! (option -g). If you compile with debug info, you can get it back with readelf:
復制代碼
[root@localhost test]# gcc a.c
[root@localhost test]# readelf -wi a.out
[root@localhost test]# gcc a.c -g
[root@localhost test]# readelf -wi a.out
The section .debug_info contains:
Compilation Unit @ offset 0x0:
Length: 135
Version: 2
Abbrev Offset: 0
Pointer Size: 8
<0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
DW_AT_stmt_list : 0
DW_AT_high_pc : 0x400453
DW_AT_low_pc : 0x400448
DW_AT_procer : GNU C 4.1.2 20080704 (Red Hat 4.1.2-55)
DW_AT_language : 1 (ANSI C)
DW_AT_name : a.c
DW_AT_comp_dir : /work/farsight/test
<1><61>: Abbrev Number: 2 (DW_TAG_subprogram)
DW_AT_external : 1
DW_AT_name : main
DW_AT_decl_file : 1
DW_AT_decl_line : 4
DW_AT_prototyped : 1
DW_AT_type : <83>
DW_AT_low_pc : 0x400448
DW_AT_high_pc : 0x400453
DW_AT_frame_base : 0 (location list)
<1><83>: Abbrev Number: 3 (DW_TAG_base_type)
DW_AT_name : int
DW_AT_byte_size : 4
DW_AT_encoding : 5 (signed)
復制代碼
ANS2:
