日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

执行目标文件引发的问题:syntax error: word unexpected (expe...

發布時間:2025/5/22 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 执行目标文件引发的问题: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...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。