绝对菜鸟在ubuntu下编译了arm的c程序
1、 直接gcc -o hciwr hciwr.c
执行程序报如下错误:
/system # ./hciwr
./hciwr: line 1: syntax error: unexpected "("
编译器的问题,可能gcc应该是x86的
2、所以用arm的gcc编译:
arm-linux-gcc-4.5.1 -o hciwr hciwr.c
3、执行2步得到的hciwr执行程序
# cp hciwr bin/
/system # hciwr
sh: hciwr: not found
应该是缺少库文件,在ubuntu下执行:
arm-linux-readelf -a hciwr
找到:Program Headers:
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
EXIDX 0x000e24 0x00008e24 0x00008e24 0x00008 0x00008 R 0x4
PHDR 0x000034 0x00008034 0x00008034 0x00140 0x00140 R E 0x4
INTERP 0x000174 0x00008174 0x00008174 0x00013 0x00013 R 0x1
[Requesting program interpreter:/lib/ld-linux.so.3]
LOAD 0x000000 0x00008000 0x00008000 0x00e30 0x00e30 R E 0x8000
LOAD 0x000f0c 0x00010f0c 0x00010f0c 0x00148 0x0014c RW 0x8000
DYNAMIC 0x000f18 0x00010f18 0x00010f18 0x000e8 0x000e8 RW 0x4
NOTE 0x000188 0x00008188 0x00008188 0x00020 0x00020 R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x4
GNU_RELRO 0x000f0c 0x00010f0c 0x00010f0c 0x000f4 0x000f4 R 0x1
PAX_FLAGS 0x000000 0x00000000 0x00000000 0x00000 0x00000 0x4
需要/lib/ld-linux.so.3这个库文件。
在ubuntu下继续找:
find -name ld-linux.so*
找到
root@axent-desktop:/home/axent/tiny210/android# find -name ld-linux.so*
./android-4.0.3_r1/prebuilt/linux-x86/toolchain/i686-linux-glibc2.7-4.4.3/sysroot/usr/lib/ld-linux.so.2
不过它只是一个连接,如下:
root@axent-desktop:/home/axent/tiny210/android/android-4.0.3_r1/prebuilt/linux-x86/toolchain/i686-linux-glibc2.7-4.4.3/sysroot/usr/lib# ls –al
出现了:
-rwxr-xr-x 1 root root 109152 2011-12-22 17:31 ld-2.7.so
lrwxrwxrwx 1 root root 9 2013-03-04 08:51 ld-linux.so.2 -> ld-2.7.so
4、需要把ld-2.7.so拷贝出来,然后命名为ld-linux.so.3
在目标机(开发板上)的根目录下建立一个lib文件夹,并ld-linux.so.3文件放在lib文件夹下,执行程序,出现下面问题:
/ # hciwr
/system/bin/sh: hciwr: Accessing a corrupted shared library
说是没有包含所有的库文件,那还需要什么库文件呢???
又一说法:执行文件可以的编译方式有动态编译和静态编译。
上述方法就是动态编译了,在执行的时候,连接相关的库文件。但是现在还是有库文件没找到。
静态编译可以将所需要的库文件都打包进去,那么通过什么方式实现静态编译呢?
终于找到方法了:
root@axent-desktop:/home/axent/tiny210/android/hciwr# arm-linux-gcc -static -o hciwr hciwr.c
生成的hciwr可执行程序为600k,而动态编译出来的可执行文件为9k左右。
-rwxr-xr-x 1 root root 618208 2013-04-02 15:23 hciwr
-rwxr--r-- 1 nobody nogroup 4156 2013-04-02 15:23 hciwr.c
拷贝到开发板上的根目录下,执行
/ # cp sdcard/hciwr ./
/ # su
/ # chmod 777 hciwr
/ # ./hciwr
< HCI Command: ogf 0x3f, ocf 0x0015, plen 3
00 00 11
> HCI Event: 0x0e plen 6
01 15 FC 00 00 00
< HCI Command: ogf 0x3f, ocf 0x0015, plen 3
05 00 1C
> HCI Event: 0x0e plen 6
01 15 FC 00 05 00
< HCI Command: ogf 0x3f, ocf 0x0015, plen 3
09 00 00
> HCI Event: 0x0e plen 6
01 15 FC 00 09 00
< HCI Command: ogf 0x3f, ocf 0x0015, plen 4
0A 00 5C 8F
> HCI Event: 0x0e plen 6
01 15 FC 00 0A 00
< HCI Command: ogf 0x3f, ocf 0x0015, plen 3
09 00 01
> HCI Event: 0x0e plen 6
01 15 FC 00 09 00
returnv =< HCI Command: ogf 0x3f, ocf 0x0015, plen 3
returnv = 0A 01 02
returnv => HCI Event: 0x0e plen 8
returnv = 01 15 FC 00 0A 01 5C 8F
ulval_l =0X5C
ulval_h =0X8F
执行成功!
那就先采用静态编译的方式吧