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

    转自原文javascript面向对象之Javascript 继承 在JavaScript中实现继承可以有多种方法,下面说两种常见的. 一,call 继承 先定义一个“人”类 //人类 Person=f ...

  2. WebApplicationContext初始化(转)

    ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:在We ...

  3. MVC地区多级联动扩展实现(非递归形式)

    MVC前台界面调用方式如下: @Html.AreaDropDownList(, string.Empty) 参数说明: 第一个参数控件的名称: 第二个参数选中的地区编码: 第三个参数地区层级: 第四个 ...

  4. 【转】selenium自动化测试环境搭建

    转:http://blog.csdn.net/mao1059568684/article/details/17347853 第一步 安装JDK JDk1.7. 下载地址:http://www.orac ...

  5. LLVM每日谈之十九 LLVM的第一本系统的书<Getting Started with LLVM Core Libraries>

    作者:史宁宁(snsn1984) LLVM最终有了一本系统的书了--<Getting Started with LLVM Core Libraries>. 这本书号称是LLVM的第一本书, ...

  6. 【iOS开发-80】Quartz2D画图简单介绍:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  7. DevExpress14.1.2 xe XE6 高速安装

    之前在在网上下载的DevExpress14.1.2 xe-XE6都是一个个包文件.须要一个个去查找编译安装,并且须要有一定的顺序要求. 所下面载了好久了都没有安装. 近期在网上找了个旧版的安装方法.以 ...

  8. UVA-1335(UVALive-3177) Beijing Guards 贪心 二分

    题面 题意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物,则双方都会很不高兴,问最少需要多少种不同的礼物才能满足所有人 ...

  9. 09-JavaScript高级

    今日知识 1. Dom(文档对象模型) 2. Bom(浏览器对象模型) 3. 总结 Dom 1. 获取id为div1的元素对象. * var result=document.getElementByI ...

  10. JavaScript的并且&&

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