歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 執行目標文件引發的問題:syntax error: word unexpected

執行目標文件引發的問題:syntax error: word unexpected

日期:2017/3/1 16:17:28   编辑:關於Linux
執行目標文件引發的問題:syntax error: word unexpected 今天不小心把一個目標文件當成了可執行文件放到開發板上進行執行,結果出現了這樣一個問題:./hello_qt: line 1: syntax error: word unexpected (expecting ")"),因為以前沒有碰到過這事,一時間有點蒙,就是一個簡單的hello world按道理不會有問題才對。於是google了一下,原來是一個小小的-c編譯選項搞得鬼。順帶也擴展學習總結了一下。 arm和pc上執行目標文件的區別 一般來說,gcc -c選項編譯出來的目標文件是不可執行的,因此也就不會遇到這種問題,尤其是在PC上就更是如此。我這邊是因為把文件轉windows工作台,再通過tftp下載的開發板上,然後文件就全部是普通文件,都是自己chmod +x改的可執行,一時大意才難得碰上這問題。 PC上執行目標文件的錯誤提示 1 ~/test$ ./zh_display.o 2 -bash: ./zh_display.o: cannot execute binary file ARM上執行交叉編譯目標文件的錯誤提示 1 $ ./hello_qt 2 ./hello_qt: line 1: syntax error: word unexpected (expecting ")") PC上的提示信息一看就懂,而ARM上的就會讓人莫名奇妙了。一開始我懷疑是自己代碼有問題,還反復檢查了一遍,幸好只是一個簡單的hello world程序,不然夠我郁悶的。也多虧我及時google,否則還不知道要浪費多少時間在這小問題上,有時候google真的很重要呀!! 區分目標文件和可執行文件 目標文件和可執行文件平時都是很容易區分的,所以一般也不會注意這個。不過從今天的問題上,我又學到了不少關於兩者差別的東西,還有區分兩者的Linux工具。 file工具:查看文件的基本屬性信息 1 ~/test$ file hello_qt 2 hello_qt: ELF 32-bit LSB executable, ARM, version 1, dynamically linked (uses shared libs), not stripped 3 4 ~/test$ file hello_qt.o 5 hello_qt.o: ELF 32-bit LSB relocatable, ARM, version 1, not stripped 兩者均是ELF文件,但是目標文件是:relocatable, 而可執行文件是: executable。 readelf工具:查看ELF文件的詳細信息 01 ~/test$ readelf -h hello_qt 02 ELF Header: 03 Magic: 7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00 04 Class: ELF32 05 Data: 2's complement, little endian 06 Version: 1 (current) 07 OS/ABI: ARM 08 ABI Version: 0 09 Type: EXEC (Executable file) 10 Machine: ARM 11 Version: 0x1 12 Entry point address: 0x87f8 13 Start of program headers: 52 (bytes into file) 14 Start of section headers: 3948 (bytes into file) 15 Flags: 0x202, has entry point, GNU EABI, software FP 16 Size of this header: 52 (bytes) 17 Size of program headers: 32 (bytes) 18 Number of program headers: 6 19 Size of section headers: 40 (bytes) 20 Number of section headers: 27 21 Section header string table index: 24 22 23 ~/test$ readelf -h hello_qt.o 24 ELF Header: 25 Magic: 7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00 26 Class: ELF32 27 Data: 2's complement, little endian 28 Version: 1 (current) 29 OS/ABI: ARM 30 ABI Version: 0 31 Type: REL (Relocatable file) 32 Machine: ARM 33 Version: 0x1 34 Entry point address: 0x0 35 Start of program headers: 0 (bytes into file) 36 Start of section headers: 1040 (bytes into file) 37 Flags: 0x200, GNU EABI, software FP 38 Size of this header: 52 (bytes) 39 Size of program headers: 0 (bytes) 40 Number of program headers: 0 41 Size of section headers: 40 (bytes) 42 Number of section headers: 16 43 Section header string table index: 13 -h選項讀取ELF文件的文件頭信息,注意其中的兩項值:Type 和 Entry point address。Type信息就是file中的文件類型,而 Entry point address表示文件的執行入口點,只有可執行文件該項才有值,而目標文件是可重定向文件,還不可以直接執行,因此該項值為0. 目標文件兩者為: 1 Type: REL (Relocatable file) 2 Entry point address: 0x0 而可執行文件兩者為: 1 Type: EXEC (Executable file) 2 Entry point address: 0x87f8
Copyright © Linux教程網 All Rights Reserved