树莓派--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 

#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

你可能感兴趣的:(Raspberry,树莓派,Linux)