樹莓派--bcm2835 library (2) 交叉編譯BCM2835

在上文中,按照guide, 在樹莓派目標板上install bcm2835.

因爲bcm2835是用戶空間應用,所以可以在宿主機上交叉編譯,生成binary後在樹莓派執行

按照guide: 

Installation

This library consists of a single non-shared library and header file, which will be installed in the usual places by make install

# download the latest version of the library, say bcm2835-1.xx.tar.gz, then:
tar zxvf bcm2835-1.xx.tar.gz
cd bcm2835-1.xx
./configure
make
sudo make check
sudo make install


這裏我們不需要安裝,只要make生成.a庫就可以。

交叉編譯:

./configure -host=arm CC=arm-linux-gnueabihf-gcc-4.9.3 ar=arm-linux-gnueabihf-ar

make

在make時出現錯誤

Bcm2835-1.45$ make
CDPATH="${ZSH_VERSION+.}:" && cd . && aclocal-1.13 -I m4
/bin/bash: aclocal-1.13: command not found
Makefile:327: recipe for target 'aclocal.m4' failed
make: *** [aclocal.m4] Error 127

解決:

1. sudo apt-get install automake

2. sudo autoreconf -ivf

然後make,無報錯


在src下生成了.a文件

~/Raspberry/bcm2835-1.56/src$ ls
bcm2835.c  bcm2835.o     Makefile     Makefile.in
bcm2835.h  libbcm2835.a  Makefile.am  test.c

將bcm2835.a 以及頭文件 bcm2835.h,  copy到led.c目錄下,編譯LED測試文件

led.c 如下

#include <bcm2835.h>

#define PIN 26
int main(int argc, char **argv)
{
    if (!bcm2835_init())return 1;
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

    while (1)
    {
        bcm2835_gpio_write(PIN, HIGH);
        bcm2835_delay(500);
        bcm2835_gpio_write(PIN, LOW);
        bcm2835_delay(500);
    }
    bcm2835_close();
    return 0;
}

makefile

CC = arm-linux-gnueabihf-gcc-4.9.3
LD = arm-linux-gnueabihf-ld

led:led.c
	$(CC) -Wall -I./ led.c -o led -L./lib -lbcm2835
clean:
	rm led

將生成的led binary文件copy到樹莓派執行, LED閃爍


http://www.airspayce.com/mikem/bcm2835/


./configure -host=arm CC=arm-linux-gnueabihf-gcc-4.9.3 ar=arm-linux-gnueabihf-ar
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章