【问题】
用arm-linux-gcc编译出来的可执行文件clkCtl,下载到板子上,在Linux下不能运行:
./clkCtl: line 1: syntax error: word unexpected (expecting ")")

【解决过程】
1.网上有人也遇到此问题:
Syntax error: word unexpected (expecting ")")
http://hi.baidu.com/dsfire/blog/item/5d922458886ad589800a188b.html
其是用ftp传到板子上的,是没有传输完整,所以导致运行有问题,
但是我是用的lrz通过secureCRT传的,确保文件是完整的。

2.以为是我烧的uImage和rootfs有问题,又重新烧写一遍,
还是不行。

3.以为编译出来的文件有问题,所以换了arm-linux-uclibc-gcc去编译,结果也还是不行。4.

4.
在一块移植了linux 的开发板上运行两个测试程序出错
http://linux.chinaunix.net/bbs/thread-1064286-1-1.html
中提到用file查看一下,所以去Linux服务器上去查看了一下,看起来好像也是OK的:
file clkCtl
clkCtl: ELF 32-bit LSB relocatable, ARM, version 1 (ARM), not stripped

甚至用了相关工具查看,好像也是OK的:
hexdump n 1 clkCtl
hexdump: n: No such file or directory
hexdump: 1: No such file or directory
0000000 457f 464c 0101 6101 0000 0000 0000 0000
0000010 0001 0028 0001 0000 0000 0000 0000 0000
0000020 0630 0000 0000 0000 0034 0000 0000 0028
。。。

objdump -a clkCtl

clkCtl:     file format elf32-little
clkCtl

objdump -f clkCtl

clkCtl:     file format elf32-little
architecture: UNKNOWN!, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000

objdump -h clkCtl

clkCtl:     file format elf32-little

Sections:
Idx Name          Size      VMA       LMA       File off Algn
0 .text         000003e8 00000000 00000000 00000034 2**2
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
1 .data         00000000 00000000 00000000 0000041c 2**0
                  CONTENTS, ALLOC, LOAD, DATA
2 .bss          00000004 00000000 00000000 0000041c 2**2
                  ALLOC
3 .rodata       00000190 00000000 00000000 0000041c 2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
4 .comment      00000012 00000000 00000000 000005ac 2**0
                  CONTENTS, READONLY
5 .note.GNU-stack 00000000 00000000 00000000 000005be 2**0
                  CONTENTS, READONLY
6 .ARM.attributes 00000010 00000000

5.百google度了N个帖子,都没有说出具体问题的,
只有一个帖子说的好像说到点子上了:
After updating to AIR SDK 1.5.1 all Flex AIR apps give "abc bytecode decoding" error
http://bugs.adobe.com/jira/browse/FBE-323

flex_sdk_3/bin/adl_lin: 1: Syntax error: word unexpected (expecting ")")

I've seen this error before and usually its a shell execution problem, but I'm not quite sure how to fix it here. Usually it's

something like change #!/bin/sh to #!/bin/bash, but since adl_lin is a binary file I'm not sure how to go about fixing it.

FYI: I'm running Ubuntu 8.04, if that is helpful in anyway.

好像是sh的问题,
但是,去/bin目录下看了,sh好像没问题。
# sh --help
BusyBox v1.13.2 (2009-05-13 11:38:15 CST) multi-call binary

No help available.

实在是无语了。。。。。

【解决办法】
刚看到这个帖子:
Cygwin and NMT toolchain
http://www.networkedmediatank.com/showthread.php?tid=20303
没注意,后来才无意发现,原来其中就说明了原因和介绍了解决办法。。。

一切软件的错误,最终都能找到其原因,此处也不例外。
最后的发现,是我在写个简单的makefile的时候,在写编译命令的时候,写成了:
$(CC) -o $(OUT_FILE) -c $(SRC_FILE)
而实际应该是去掉-c选项,直接编译成可执行文件:
$(CC) -o $(OUT_FILE) $(SRC_FILE)

之前就一直搞混淆了-c参数,以为后面正好跟c文件。。。
现在又弄错了。。。

其中gcc的-c选项的意思是:
-c
  只激活预处理,编译,和汇编,也就是他只把程序做成obj文件
  例子用法:
  gcc -c hello.c
  他将生成.o的obj文件

也就是,如果不加-c,默认就直接编译生成可执行文件了
加上-c就只编译成目标obj文件,就不往下继续编译成可执行文件了。。。

我们实际的命令
arm-linux-gcc -o clkCtl -c clock_control.c
的意思是,将clock_control.c编译成目标文件,此输出的(目标)文件叫做clkCtl
其实如果此处不写-o clkCtl,一般默认输出的文件名是和C文件名一样,只是后缀是.o,此处即clock_control.o的。

arm-linux-gcc -o clkCtl clock_control.c
的意思是,将clock_control.c编译成可执行文件(就是先编译成目标文件然后再链接成可执行文件)
其中输出的文件名是clkCtl,如果不加上-o clkCtl ,那么Linux默认输出文件名就是a.out了,
容易混淆而且容易被覆盖,也没实际意义,所以一般加上-o 制定输出的可执行程序的文件名。

另外,说一下,gcc还有个参数是大写的C:
-C
  在预处理的时候,不删除注释信息,一般和-E使用,有时候分析程序,用这个很方便的
注意不要和这个小写的c混淆了。。。

【附录】
gcc参数详解
http://man.lupaworld.com/content/develop/UNIX_system_develop_gcc.htm

【总结】
其他朋友们,遇到这个问题,估计不少也是和我一样,
就是在编译的时候,错误地加入了-c 选项,使得生成出来的不是可执行文件,是目标文件,
所以运行的时候,就无法运行了。

感慨一句,这么基本的基础知识都忘了,丢人啊。。。。

【后记】

其实是自己的相关知识还是不够,把目标文件的信息,看成是可执行文件的信息了,因为刚才又去用file看了真正可执行文件的信息,和之前file查看目标文件的结果是不一样的:

[crifan@localhost clock_control]$ file clock_control.o
clock_control.o: ELF 32-bit LSB relocatable, ARM, version 1 (ARM), not stripped
[crifan@localhost clock_control]$ file clkCtl
clkCtl: ELF 32-bit LSB executable, ARM, version 1 (ARM), dynamically linked (uses shared libs), not stripped
[crifan@localhost clock_control]$

如果之前就具备这些知识,那么也就可以看出毛病了。还有,如果脑子灵活点,用file去查看其他已经有的可执行文件,然后和上面的比较,也是可以看出来的。还是方法不当啊。。。

同样,用readelf工具查看也可以看清楚具体信息:

[crifan@localhost clock_control]$ readelf -h clkCtl
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:               0x854c
Start of program headers:          52 (bytes into file)
Start of section headers:          5036 (bytes into file)
Flags:                             0x2, has entry point, GNU EABI
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:         31
Section header string table index: 28
[crifan@localhost clock_control]$ ls
clkCtl clock_control.c clock_control.h clock_control.o clock-dev.ko makefile ReadMe.txt
[crifan@localhost clock_control]$ readelf -h clock_control.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:          1584 (bytes into file)
Flags:                             0x0
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:         12
Section header string table index: 9

可执行程序无法在Linux上运行,显示line 1: syntax error: word unexpected (expecting ") .的更多相关文章

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

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

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

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

  3. -bash: /etc/profile: line 11: syntax error near unexpected token `$'{\r''报错问题解决

    在Linux系统配置Java环境变量之后执行 source /etc/profile指令报:-bash: /etc/profile: line 11: syntax error near unexpe ...

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

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

  5. 写shell,运行出错:syntax error near unexpected token `$’do\r”

    cygwin下面写shell,运行出错:syntax error near unexpected token `$’do\r” 写shell,运行出错:syntax error near unexpe ...

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

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

  7. 【无私分享:ASP.NET CORE 项目实战(第十章)】发布项目到 Linux 上运行 Core 项目

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 ASP.Net Core 给我们带来的最大的亮点就是跨平台,我在我电脑(win7)上用虚拟机建了个 CentOS7 ,来演示下 ...

  8. Wine——在Linux上运行Windows软件

    官网:https://www.winehq.org/ 参考: wikipedia 教你使用Wine在Linux上运行Windows软件 如何安装和使用Wine,以便在Linux上运行Windows应用 ...

  9. 在Linux上运行C#

    众所周知,C#是Microsoft推出的.NET语言,只能在.NET平台上运行,例如Win 9x.ME.NT.2000.XP和Win CE之类的操作系统.但是,现在却有了一个叫做Mono的项目,它的目 ...

随机推荐

  1. 【cl】selenium实例一:打开百度,获取第四个标题

    /*创建类的时候是TestNG Class*/ package Selenium_lassen; import static org.junit.Assert.*; import java.util. ...

  2. android 系统签名【转】

    本文转载自:http://blog.csdn.net/csh86277516/article/details/73549824 Android——编译release版签名系统 AndroidManif ...

  3. getElementById和querySelector区别

    1.常见的获取元素的方法有3种,分别是通过元素ID document.getElementById('idName');.通过标签名字document.getElementsByTagName(tag ...

  4. 爬虫中之Requests 模块的进阶

    requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...

  5. 第6章 服务模式 在 .NET 中实现 Service Interface

    上下文 您 的应用程序部署在 Microsoft Windows? 操作系统上.您决定将应用程序的某一块功能作为 ASP.NET Web Service 公开.互操作性是一个关键问题,因此您无法使用仅 ...

  6. Git Learning Part III - working remotely (Github)

    help document of Github : https://help.github.com/ 1 upload 1.1 new update  Initialize a repository  ...

  7. 关于各浏览器下Hack的写法

    下面是我收集有关于各浏览器下Hack的写法: 1.Firefox @-moz-document url-prefix() { .selector { property: value; } } 上面是仅 ...

  8. Javascript Proxy对象 简介

    Javascript Proxy对象 简介 Javascript Proxy对象 改变你操作对象的方式 Proxies 是Javasript对象的中间件 ...或者说至少是那种很早的版本. ES6 中 ...

  9. 利用JavaScript实现文本框改文字功能

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

  10. mybatis学习笔记之基础框架(2)

    mybatis学习笔记之基础框架(2) mybatis是一个持久层的框架,是apache下的顶级项目. mybatis让程序将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成满足s ...