【Raspberry Pi】VSCODE入れてみたらハマった。C言語のデバック付き開発環境を作ってみる【Windows/Ubuntu】

スポンサーリンク
広告

Raspberry Pi 3B+になったのでIDE入れてみる

クロス環境が良いなといいつつ。。3B+が出て、Raspberry Piも早くなったので、IDEを組み込んでみようかと。。 といいつつ。。実は、とあるセンサーを使おうとしてDebugがうまくいかなくて入れてみようか?って思ったというのが・・正直なところ(笑) I2Cのセンサなんですが、レジスタが公開されてないので、Pythonとかで直打ちもできず。。1個だけならうまく動くんですが、I2Cのアドレス変更するとうまく動かない。。それをデバッグしてみようかと。。

ちなみに。。NoobsにはGeanyというデバックモードがないIDEが入っているし、エディタは、Leafpad、コンパイラのgccや、デバッガーのgdbも入っています。

Raspberry Piに入りそうなIDEは?

EclipseVSCODENetbeansあたりが、使われていそうですね。でもググってみると、どちらかというとクロス環境を作る用の説明が多いです。例えば

Raspberry PiのクロスコンパイルとVSCodeによるリモートデバッグ開発環境の構築 - Qiita
この記事について 以前、この記事( )で、お手軽にラズパイ用のC/C++開発環境の構築を行いました。この時は、ビルドやデ...

その中で、以下の記事を見つけましたので。

Raspberry PiのC++開発環境を Visual Studio Codeで構築 | ある計算機屋さんの手帳
Raspberry Pi3のC++開発環境をVS Codeで構築 2018年3月、Raspberry Pi3のC++開発環境をVisual Studio Code(VS Code)で実現しました。 少し前に、Window

まずはVSCODEを試してみることにしました。

注意:そのままでは、デバッグができないので、かなりいろいろインストールやら修正がいります!ちょっと、やってみようという方は、これ以上進まない方がいいですよ(笑)

Visual Studio Code (VSCODE)のインストール

Raspberry Pi 用のバイナリ―が既にあって、Community builds of Visual Studio Codeにあります。

このページのLinuxバージョンに飛ぶとスクリプトがあります。

まずは、Superuserになってから

sudo -s

 APT instructions

(including Debian, Raspbian, Ubuntu and Linux Mint)

. <( wget -O - https://code.headmelted.com/installers/apt.sh )

このコマンドを、コピペしてリターンです。

そうすると・・なんか必要なパッケージが足りないとか言われたのですが。。その通り

sudo apt-get なんたらLib  だったような。。をコピペしてインストールしたらVSCODEができてました。

OSS-CODEという名前です。

しばらくすると・・こういう画面が出てきます。

ちなみにバージョンはこちら

バージョン1.14.0
コミット05b254dcfb44422238c6fdb665721a41ee4ce6ac
日付2017-06-20T20:22:03.289Z
シェル1.6.6
レンダラー56.0.2924.87
Node 7.4.0

gcc、gdbなどのバージョン

Linuxのバージョン(3B+で使ってます)

pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.4 (stretch)
Release: 9.4
Codename: stretch

gcc

pi@raspberrypi:~ $ gcc --version
gcc (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gdb

pi@raspberrypi:~ $ gdb --version
GNU gdb (Raspbian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

make

pi@raspberrypi:~ $ make --version
GNU Make 4.1
このプログラムは arm-unknown-linux-gnueabihf 用にビルドされました
Copyright (C) 1988-2014 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 以降 <http://gnu.org/licenses/gpl.html>
これはフリーソフトウェアです: 自由に変更および配布できます.
法律の許す限り、 無保証 です.

もしこれらのコンパイラ類が入ってない場合は、

pi@raspberrypi:~ $ sudo apt install build-essential

pi@raspberrypi:~ $ sudo apt install gdb

でgcc、make、gdbがインストールされます。

VSCODEの設定

統合ターミナルを許可(Terminal でBashが動くようにする)

表示―コマンドパレット で

shellと打ち込み、 ターミナル:ワークスペースでシェルを構成することを許可する を選ぶ

表示ー統合ターミナル を選択する

VSCODE内で、Shell(Bash)が動くようになります。

簡単なコンパイル・実行(Hello World)

とりあえず、こんなソースを

#include <stdio.h>

int main(int argc, char **argv) {
  printf("Hello!\n");
  printf("World!\n");
};

適当なディレクトリ名前をつけて保存します。

例えば・・こんなディレクトリを作っておきます。

/home/pi/testclang/test1

ファイルーフォルダーを開く で、 上のディレクトリを指定します。

TEST1のディレクトリに、helloworld.c という新規ファイルを作って、上のソースを入れます。

IDEらしくないけど普通に動かして確認してみる(笑)

統合ターミナル(表示―統合ターミナル)を開いて、手でコマンド打ちます(笑)

pi@raspberrypi:~/testclang/test1 $ gcc helloworld.c
pi@raspberrypi:~/testclang/test1 $ ./a.out
Hello!
World!

今日の格言 環境構築は、いちいち確実に動く方法からやった方がいいです(笑) 

これで、ソースファイルも、一応、gccも動くことが確認できました。

次はデバッガが動くか?

gccに、次のオプションを付けます。

  • -g : コンパイル,リンク時にDEBUG情報を付加する
  • -O0 : 最適化をしません
pi@raspberrypi:~/testclang/test1 $ gcc -g -O0 helloworld.c

でgdbで動かしてみます。

main にブレークポイントを入れて(b main )、 動かして(run)、ブレークして、(n)で一行づつ動かす。

pi@raspberrypi:~/testclang/test1 $ gdb ./a.out
GNU gdb (Raspbian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.

(gdb) b main 
Breakpoint 1 at 0x10450: file helloworld.c, line 4.
(gdb) run
Starting program: /home/pi/testclang/test1/a.out

Breakpoint 1, main (argc=1, argv=0x7effee24) at helloworld.c:4
4 printf("Hello!\n");
(gdb) n
Hello!
5 printf("World!\n");
(gdb) n
World!
6 };(gdb)

まぁ、これでやってたら、きりがなくなってきたので、VSCODEを入れたのですが(笑)

IDEっぽくやってみます

最初に、当該ディレクトリのa.outは消しておきましょう(笑) できてるって錯覚しちゃうので(笑)

Build Taskを設定します 下を見て。。テンプレができます。

task.json を以下のように書き換えて、make コマンドを動かすようにします。

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "make",
"isShellCommand": true,
"showOutput": "always",
"echoCommand": true ,
"tasks": [
{ "taskName" : "MyBuild" ,"suppressTaskName": true , "args": [] ,"isBuildCommand": true}
]
}

Makefile も作っておかないといけないですね。

CC := gcc
CFLAGS := -g -O0

helloworld:

clean:
    $(RM) helloworld

とっても簡単なのにしておきます(笑)

これで、Build Taskをクリックするとmakeされて、今度は実行形式のファイル helloworldが出来ているので、ターミナルで実行してみます。

pi@raspberrypi:~/testclang/test1 $ ./helloworld
Hello!
World!

出来ているのを確認できましたので、VSCODEから動かすようにします。今度は、test task で動くようにします。

まずは、Testtaskの記述を task.jsonに書きます。1行追加です。

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "make",
"isShellCommand": true,
"showOutput": "always",
"echoCommand": true ,
"tasks": [
{ "taskName" : "MyBuild" ,"suppressTaskName": true , "args": [] ,"isBuildCommand": true},
{ "taskName" : "MyTest" ,"suppressTaskName": true , "args": ["exec"] ,"isTestCommand": true}
]
}

makeコマンドからexec をつけて動くということになっているので makeにexec:を追加します。

CC := gcc
CFLAGS := -g -O0

helloworld:

clean:
$(RM) helloworld

exec:
./$ helloworld

これで、やっとIDEっぽくなりました。昔のVCとかVBとか懐かしいなぁ~って思いながら(笑) Tasks の Test Taskで。。動いていますよね。。ソースちょこっと変わってるけど(笑)

この方法だと、makefileの記述方法知ってれば、なんとなくできるようになりますね。

Debugをしてみる。

やっと本題に入れますが・・・ここからが難題でした(笑)

まず、少しDebugっぽくなるようにFor文などを入れたソースにしておきます。

機能拡張でC/C++ InteliSense, debugging and code browsingをインストールします。

それから、デバッグの開始 をクリックしてみます。

また今度は、launch.json を書けと言われます。修正点は、

 "program": "enter program name, for example ${workspaceRoot}/a.out",

となっている所を

 "program": "${workspaceRoot}/helloworld",

って変えるだけですが・・ デバッグを開始すると。。エラーでます(笑)

これを解決できたのは、この方のおかげです。感謝します。

Raspberry PiのC++開発環境を Visual Studio Codeで構築 | ある計算機屋さんの手帳
Raspberry Pi3のC++開発環境をVS Codeで構築 2018年3月、Raspberry Pi3のC++開発環境をVisual Studio Code(VS Code)で実現しました。 少し前に、Window

問題は、

  1. monoランタイム(.NetFrameworkのラインタイム)の mono-sgenを /…/debugAdapters/mono.linux にコピーする
  2. GnoneTerminalを入れる

と解決します。

$ mkdir mono_temp
$ cd mono_temp
$ wget http://download.mono-project.com/repo/debian/pool/main/m/mono/mono-runtime-sgen_4.2.3.4-0xamarin1_armhf.deb
パッケージをインストールして
$ dpkg -x mono-runtime-sgen_4.2.3.4-0xamarin1_armhf.deb .
mono-sgenがあるか確認
$ ls usr/bin
mono-sgen
所定の位置にコピーします。
$ cp usr/bin/mono-sgen /home/pi/.vscode-oss/extensions/ms-vscode.cpptools-0.14.0/debugAdapters/mono.linux

次は、Gnone-terminalのインストール

$ sudo apt-get install gnome-terminal

これらを実施すれば、とりあえず、DebugをVSCODE上でできるようになりました。

ブレークポイントを入れて、ひとつづく動かしていきます。なんか懐かしいですね。

ふぅ。。。できた! けど。。やりたいのは、このプログラムじゃないので。。ここからですけど(笑)

これで原因わかるといいんだけどなぁ~

コメントを残していただけるとありがたいです

Loading Facebook Comments ...
%d人のブロガーが「いいね」をつけました。