可執行程序無法在Linux上運行,顯示line 1: syntax error: word unexpected (expecting ") .


【問題】
用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM