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