6
20

More than 5 years have passed since last update.

Raspberry PiをC言語で動かしてみる

Last updated at Posted at 2018-04-08

ラズベリーパイでプログラムを開発するにはPythonかJavaを覚えないといけないような気がしていつもそこで止まっていた。
今日、よく考えたらgccが使えるのでC言語で開発すればいいと今更ながら思いついた。

GPIOでLEDの点滅(C言語)

上記の記事を参考にC言語でLチカを組んでみた。
まずはラズベリーパイ上でgitで「wiringPi」ライブラリをインストール&コンパイル

$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ ./build

サンプルソースをviで打ち込んでgpio_test.cで保存

#include <wiringPi.h>
#define GPIO17 17

int main(void) {
int i;

if(wiringPiSetupGpio() == -1) return 1;
    pinMode(GPIO17, OUTPUT);
    for(i=0; i<10; i++){
        digitalWrite(GPIO17, 0);
        delay(950);
        digitalWrite(GPIO17, 1);
        delay(50);
    }
    digitalWrite(GPIO17, 0);

    return 0;
}

以下を打ち込んでgccでコンパイルしてみるけど、
$ gcc -o gpio_test gpio_test.c -I/usr/local/include -L/usr/local/lib -lwiringPi

'wiringPiSetupGpio'に関する定義されていない参照です
・・・
とエラーになる。たぶんライブラリがちゃんとリンクされていないんだろうなと色々見てみると、上記の-lwiringPiを-IwiringPiとTypoしていた。
小文字のLと大文字のIの書き間違いでした。小文字のLが正解です。

そこを修正するとコンパイルに成功したので
./gpio_test
と打つと
wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem:Permission denied.

とエラーになる。
なんか実行権限がいるとか書いてあった記憶があったので

sudo ./gpio_test

と打つと無事動きました。

6
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
20