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

    //本来写了个和1021相同的HASH,但没过,于是,抱着侥幸的心理,把它变成距离的四次方, //我就呵呵了... //这个题,完全靠概率.当然了,如果是把图翻转来比较,也是可以的.但好像很麻烦.. ...

  2. Android 源代码解析 之 setContentView

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41894125,本文出自:[张鸿洋的博客] 大家在平时的开发中.对于setCont ...

  3. 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description ...

  4. jquery非文本框复制

    function selectText(x) { if (document.selection) { var range = document.body.createTextRange();//ie ...

  5. 写一个类似淘宝的ios app需要用到哪些技术?

    写一个类似淘宝的ios app需要用到哪些技术? 让我想起了有人私信我,说不缺钱,做个类似知乎的东西,包括加运营,需要多少钱. 扯淡结束,正好最近看了一点这方面的东西,也许对题主来说有点帮助. 手机淘 ...

  6. 样条函数(spline function)—— 分段多项式函数(piecewise polynomial function)

    1. 分段多项式函数 样条函数是某种意义上的分段函数. Spline (mathematics) - Wikipedia 最简单的样条函数是一种分段多项式函数(piecewise polynomial ...

  7. 杂项:DS(目录服务)

    ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...

  8. Redis List 命令技巧

    1.实现栈的功能(先进后出) lpush + lpop = stack > lpush mylist (integer) > lpop mylist " > lpop my ...

  9. 部署hexo后github pages页面未更新或无法打开问题

    title: 部署hexo后github pages页面未更新或无法打开问题 date: 2018-03-30 15:34:29 categories: methods tags: hexo gith ...

  10. Delete, drop table, truncate之间的区别

    Delete, drop table, truncate有什么区别? delete 删除表中数据,可以删除一条或多条记录,可以回滚,记录操作日记,是DML truncate table,一次性清空表中 ...