今天不小心把一个目标文件当成了可执行文件放到开发板上进行执行,结果出现了这样一个问题:./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

执行目标文件引发的问题:syntax error: word unexpected (expe...的更多相关文章

  1. line: 1: Syntax error: word unexpected (expecting ")")

    开发板上运行可执行程序报出错误: line1: 1: Syntax error: word unexpected (expecting ")") 解决思路: 1.编译器的问题 用a ...

  2. 可执行程序无法在Linux上运行,显示line 1: syntax error: word unexpected (expecting ") .

    [问题]用arm-linux-gcc编译出来的可执行文件clkCtl,下载到板子上,在Linux下不能运行:./clkCtl: line 1: syntax error: word unexpecte ...

  3. line1: 1: Syntax error: word unexpected (expecting ")")

    行时错误:line1: 1: Syntax error: word unexpected (expecting ")") 查看Makefile编译规则,可能由于依赖关系造成编译器使 ...

  4. 执行shell脚本报错:syntax error near unexpected token `或syntax error: unexpected end of file

    引起此问题最可能的原因是: 在windows下编写的文件上传到linux执行. 我是在notepad++上编写的代码,之后上传到linux执行,报此错误.仔细检查,语法方面没有错误.上网查了一下,发现 ...

  5. 解决Linux下编译.sh文件报错 unexpected operator Syntax error: word unexpected

    执行一个脚本  发现报语法错误,但是在其他机器上运行都没有问题 唯一的区别就是 一个是centos机器  报错的是ubuntu 网上搜索了一下 因为Ubuntu默认的sh是连接到dash的,又因为da ...

  6. 执行shell脚本提示“syntax error near unexpected token for((i=0;i<$length;i++))”

    sh脚本例如以下: #!/usr/bin/env bash county="3 4 5 6 7 8 9 10 11 12 16 29 39 44 53 62 72 84 97 115 128 ...

  7. linux后台执行./run.py提示python syntax error near unexpected token `('

    python脚本中的#!/usr/bin/python     估计有不少人注意过一些python脚本开头有这么行东东: #!/usr/bin/python 它是用来干嘛的?貌似没有它对脚本功能也没啥 ...

  8. 解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题

    参考:https://blog.csdn.net/u012453843/article/details/69803244 解决执行脚本报syntax error: unexpected end of ...

  9. Linix登录报"/etc/profile: line 11: syntax error near unexpected token `$'{\r''"

    同事反馈他在一测试服务器(CentOS Linux release 7.2.1511)上修改了/etc/profile文件后,使用source命令不能生效,让我帮忙看看,结果使用SecureCRT一登 ...

随机推荐

  1. 经验总结18--EF改动关系,多对多

    EF改动关系让我费事蛮多时间.能查的资料少,网上试了非常多方法都不正确. 最后还是自己研究出来了.在这里和大家分享下,有更好的方法也能够分享下. 首先说说我一般做改动功能时,前台传參数,后台使用对象接 ...

  2. Narrow Art Gallery

    Time Limit: 4000ms, Special Time Limit:10000ms, Memory Limit:65536KB Total submit users: 11, Accepte ...

  3. Asura监控---AsuraMonitor,阿修罗监控开源

    阿修罗Monitor是一个功能强大.灵活的监控系统. 系统安装简单,配置简单,相比zabbix, nagios,cacti,小米监控等都使用相当简单.只需要会写脚本,语言不限就可以实现任意监控需求. ...

  4. .net core发布到IIS后502.5错误

    net core 在win7系统发布后,出现在502.5错误. 打开“开始”菜单,搜索“事件查看器”,然后选择“事件查看器”应用. 在“事件查看器”中,打开“Windows 日志”节点. 选择“应用程 ...

  5. BZOJ 1443 二分图博弈 网络流

    思路: 二分图博弈嘛 找到最大匹配的必须点 跑个网络流 前后DFS一遍 //By SiriusRen #include <queue> #include <cstdio> #i ...

  6. C - Twins(贪心)

    Problem description Imagine that you have a twin brother or sister. Having another person that looks ...

  7. 使用BindingList来实现DataGridview数据源为list时的动态增删改

    当DataGridview的数据源list的时候,对list进行操作后重新绑定,数据并不会更新 使用BindingList能很好的解决这个问题(framework2.0新增) 例如,使用list时候的 ...

  8. 利用JavaScript的%做隔行换色

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  9. 打开word2010每次都要配置进度的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 不小心把ms office2010搞坏了,于是重装ms office2010,结果一打开word文档时总是出现下面的对 ...

  10. Android stroke 边框线 某一边

    有时候需要给View加边框线,我们经常是四边一起加,就像这样: <shape xmlns:android="http://schemas.android.com/apk/res/and ...