linux菜鳥在ubuntu下編譯了arm的c程式

2021-06-13 10:53:52 字數 3672 閱讀 9778

絕對菜鳥在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  *** 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

執行成功!

那就先採用靜態編譯的方式吧

ubuntu菜鳥筆記

linux 基礎 linux 的靈感源自 1969 年就出現的 unix 作業系統,時至今日該系統仍被廣泛使用,並在不斷發展中。unix 背後的許多設計慣例也同樣存在於 linux 中,對系統基本原理的理解至關重要。unix 最初主要使用命令列介面,這在 linux 中也得到了保留。也就是說,圖形使...

在Ubuntu下安裝openGL

首先安裝必要的庫,開源的 mesa提供了與opengl庫完全相同的介面,使用下面的命令安裝 sudo apt get install freeglut3 freeglut3 dev libglut3 libglut3 dev libgl1 mesa libgl1 mesa dev 使用命令dpkg ...

在ubuntu下安裝vmware tools 總結

今天在ubuntu 下安裝vmware tools 在網上找了好多資料,頭都看暈了,終於得出一點思路。就是 在安裝完ubuntu 系統後,找到要安裝的vmware tools 的壓縮檔案,然後 執行其中的可執行檔案 在我裝vmware tools時 具體步驟如下 在虛擬光碟機中,mount linu...