Syntax error: word unexpected (expect

Today, I accidentally took an object file as an executable file and put it on the development board for execution. As a result, there is such a problem:./Hello_ QT: Line 1: syntax error: word unexpected (expecting “)”, because I haven’t encountered this before, it’s a bit confusing for a moment, that is, a simple hello world will not have any problems according to reason. So Google found out that it was a small – C compilation option that got the hell out of it. By the way, I also extended my study and summarized it

The difference of executing object file between arm and PC

Generally speaking, the target file compiled by GCC – C option is not executable, so this problem will not be encountered, especially on PC. On my side, I transfer the files to the windows workbench, download them to the development board through TFTP, and then all the files are ordinary files, which can be executed by Chmod + X. it’s hard to encounter this problem when I’m careless

Error prompt of executing target file on PC

~/test$ ./zh_display.o
-bash: ./zh_display.o: cannot execute binary file

Error prompt of cross compiling object file on ARM

$ ./hello_qt 
./hello_qt: line 1: syntax error: word unexpected (expecting ")")

The prompt information on PC can be understood as soon as you read it, while the prompt information on arm can make people feel strange. At the beginning, I suspected that there was something wrong with my code, and I checked it again and again. Fortunately, it was just a simple hello world program, otherwise I would be depressed enough. Thanks to Google in time, otherwise I don’t know how much time I have to waste on this small problem. Sometimes Google is really important!!

Distinguish between object file and executable file

The object file and executable file are usually very easy to distinguish, so they generally do not pay attention to this. However, from today’s problem, I have learned a lot about the differences between the two, and the Linux tools to distinguish them

File tool : view the basic attribute information of a file

~/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

Both are elf files, but the target file is: relocatable , and the executable file is: executable

readelf tool : view the details of ELF file

~/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

-The H option reads the file header information of the ELF file, paying attention to the two values: type and entry point address. The type information is the file type in the file, and the

Entry point address indicates the execution entry point of a file. Only the executable file has a value for this item. The target file is a redirectable file and cannot be executed directly. Therefore, the value of this item is 0

The target files are:

Type:                              REL (Relocatable file)
Entry point address:               0x0

The executable files are:

Type:                              EXEC (Executable file)
Entry point address:               0x87f8

Similar Posts: