WindowsでのRaspberry PI向けクロスコンパイルで、VS2015でcan-utilsのcandumpをデバッグ


- HelloWorldをWin上でクロスコンパイル`=>` scpで実行ファイルをRasPiにコピー `=>` Visual Studio 2015でデバッグ。
- [`can-utils`](https://github.com/linux-can/can-utils)というのをクロスコンパイル、デバッグ。(CAN moduleをRasPiにつなぐ記事も書いたので..)

をやってみる。


## 動機
`raspberry pi windows cross compile`で、ググるってみていくと、
[Windows toolchain for Raspberry/PI](http://gnutoolchains.com/raspberry/) というのがあるらしい。つかってみよう。

## ツールの準備
1. MinGW/MSYSを入れといたほうがよさそうなので、インストールする。
 [MSYS2](http://sourceforge.net/projects/msys2/)を入れた。(というか、前に入れてあった)
2. [Windows toolchain for Raspberry/PI](http://gnutoolchains.com/raspberry/) から、raspbian-jessie向けとwheezy向けがあるので、自分の環境に合わせてダウンロードして、インストールする。`C:\SysGCC\Raspberry`に解凍されて、オプションによっては、`C:\SysGCC\Raspberry\bin`にPATHが張られる。
3. Puttyもいれておく。
4. Raspberry Piも

## HelloWorld

### ファイル作成
|- |File              |説明 |
|:-:|---|---|
|1 |`HelloWorld.cpp` | C++ ソース|
|2 |`Makefile` | Makefile<br> `copy_to_rasp`で、ビルド後、PuttyのPSCPで、らずぱいへコピー |
|3 |`build.bat` | `make`呼び出し前にPATHとか環境変数とか設定 |
|4 |`LaunchOptionRaspberryPi.xml` |VS2015からMIDebugEngineを使ってデバッグする  |

- 1

```cpp:HelloWorld.cpp
#include <cstdio>
int add(int x, int y)
{
        return x + y;
}
int main()
{
        int a = 5;
        int b = 4;
        int c = add(a, b);
        std::printf("Hello World %d\n", c);
        return 0;
}
```

- 2
`copy_to_rasp`では、ワークディレクトリを作って、そこにファイルをscpでコピーして、実行属性をつけて、`ls -l`で表示をしている。

```makefile:Makefile
CROSS_COMPILE=arm-linux-gnueabihf-
PUTTY_SCP=C:\Tools\putty\pscp.exe
PUTTY_PLINK=C:\Tools\putty\plink.exe
SSH_KEY=C:\Users\User01\.ssh\id_rsa-20151002.ppk

RASP_IP=raspberrypi
RASP_WORKDIR=/home/pi/dev

TARGET=HelloWorld

all: copy_to_rasp

copy_to_rasp: HelloWorld
	$(PUTTY_PLINK) -i $(SSH_KEY) pi@$(RASP_IP) mkdir -p  $(RASP_WORKDIR)
    $(PUTTY_SCP)   -i $(SSH_KEY) $(TARGET) pi@$(RASP_IP):$(RASP_WORKDIR)
	$(PUTTY_PLINK) -i $(SSH_KEY) pi@$(RASP_IP) chmod 755 $(RASP_WORKDIR)/$(TARGET)
	$(PUTTY_PLINK) -i $(SSH_KEY) pi@$(RASP_IP)     ls -l $(RASP_WORKDIR)/$(TARGET)

$(TARGET): HelloWorld.cpp
	$(CROSS_COMPILE)gcc -O0 -g -o $@ $<

clean:
	rm -rf $(TARGET) *.o

rebuild: clean $(TARGET)
```

- 3 最小限のPATHを設定... (コンパイル時エラーが出た. 想定外のDLLを使ってしまっているのだろう)

```bat:build.bat
PATH=C:\SysGCC\Raspberry\bin;C:\Windows;C:\Windows\system32;C:\msys64\mingw32\bin;D:\msys64\usr\bin

SET CROSS_COMPILE=arm-linux-gnueabihf-
SET CC=%CROSS_COMPILE%gcc
SET AS=%CROSS_COMPILE%as
SET LD=%CROSS_COMPILE%ld

SET EXITCODE=0

bash -c "make %1 %2 %3" 2> build_log.txt
IF ERRORLEVEL 1 (
    SET EXITCODE=1
)

sed -e "s/:\([0-9]*\):\([0-9]*\):/(\1,\2):/g" build_log.txt
rm -f build_log.txt
exit /b %EXITCODE%

```

- 4 

```xml:LaunchOptionRaspberryPi.xml
<PipeLaunchOptions xmlns="http://schemas.microsoft.com/vstudio/MDDDebuggerOptions/2014"
    PipePath="C:\Tools\putty\plink.exe"
    PipeArguments="-i C:\Users\user01\.ssh\id_rsa-20151002.ppk pi@raspberrypi -batch -t gdb --interpreter=mi"
    ExePath="/home/pi/dev/HelloWorld"
    ExeArguments=""
    TargetArchitecture="X64"
    WorkingDirectory="/home/pi/dev"
    AdditionalSOLibSearchPath="">
</PipeLaunchOptions>
```

### プロジェクト作成
- ファイルを準備して、`File > New > Project From Existing Code...`から、プロジェクトを作る。下にスクショを張ったので、参考に。

 ![1.png](https://qiita-image-store.s3.amazonaws.com/0/75594/c9b9d868-b4ab-8311-6eaf-9dad6db1e9d3.png)
 
![2.png](https://qiita-image-store.s3.amazonaws.com/0/75594/19475fa6-522e-64df-9b88-947f1a8ee7a9.png)
 
![3.png](https://qiita-image-store.s3.amazonaws.com/0/75594/48fe2819-5268-6be1-ad20-89cfb746ade9.png)

### ビルド
- `CTRL+SHIFT+B'

```text:[Output]タブ
1>------ Build started: Project: HelloWorld_RasPiCross, Configuration: Debug Win32 ------
1>  arm-linux-gnueabihf-gcc -O0 -g -o HelloWorld HelloWorld.cpp
1>  C:\Tools\putty\plink.exe -i C:\Users\User01\.ssh\id_rsa-20151002.ppk pi@raspberrypi mkdir -p  /home/pi/dev
1>  C:\Tools\putty\pscp.exe   -i C:\Users\User01\.ssh\id_rsa-20151002.ppk HelloWorld pi@raspberrypi:/home/pi/dev
1>
1>  HelloWorld                | 4 kB |   4.0 kB/s | ETA: 00:00:01 |  39%
1>  HelloWorld                | 10 kB |  10.1 kB/s | ETA: 00:00:00 | 100%
1>  C:\Tools\putty\plink.exe -i C:\Users\User01\.ssh\id_rsa-20151002.ppk pi@raspberrypi chmod 755 /home/pi/dev/HelloWorld
1>  C:\Tools\putty\plink.exe -i C:\Users\User01\.ssh\id_rsa-20151002.ppk pi@raspberrypi     ls -l /home/pi/dev/HelloWorld
1>  -rwxr-xr-x 1 pi pi 10316 Oct 13 14:12 /home/pi/dev/HelloWorld
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
```

### Debug
- ブレークポイントをはる。
- Command Windowを開く(`CTRL+ALT+A`)

```text
Debug.MIDebugLaunch /Executable:HelloWorld /OptionsFile:C:\VS2015\HelloWorld_RasPiCross\LaunchOptionRaspberryPi.xml
```
![4.png](https://qiita-image-store.s3.amazonaws.com/0/75594/1ab177b2-59dd-3b38-76f8-9b5f0a296cad.png)
で、[Enter]をおすと、
![5.png](https://qiita-image-store.s3.amazonaws.com/0/75594/797de0e0-dddb-719b-f0c1-6ddff30e199a.png)


## can-utils
- 1 ソースを持ってくる。

```text
C:\VS2015>git clone https://github.com/linux-can/can-utils.git
Cloning into 'can-utils'...
remote: Counting objects: 1114, done.
Receiving objects
Receiving objects: 100% (1114/1114), 321.10 KiB | 600.00 KiB/s, done.
Resolving deltas: 100% (728/728), done.
Checking connectivity... done.
```

- 2 build.bat をこのフォルダにコピーして、build.batしてみる

```text
C:\VS2015>cd can-utils

C:\VS2015\can-utils>copy ..\HelloWorld_RasPiCross\build.bat .
        1 file(s) copied.

C:\VS2015\can-utils>build.bat

C:\VS2015\can-utils>PATH=C:\SysGCC\Raspberry\bin;C:\Windows;C:\Windows\system32;
C:\msys64\mingw32\bin;D:\msys64\usr\bin

C:\VS2015\can-utils>SET CROSS_COMPILE=arm-linux-gnueabihf-

C:\VS2015\can-utils>SET CC=arm-linux-gnueabihf-gcc

C:\VS2015\can-utils>SET AS=arm-linux-gnueabihf-as

C:\VS2015\can-utils>SET LD=arm-linux-gnueabihf-ld

C:\VS2015\can-utils>make
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   can-calc-bit-timing.c   -o can-calc-bit-timing
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o candump.o candump.c
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o lib.o lib.c
arm-linux-gnueabihf-gcc   candump.o lib.o   -o candump
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   cansniffer.c   -o cansniffer
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o cansend.o cansend.c
arm-linux-gnueabihf-gcc   cansend.o lib.o   -o cansend
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o canplayer.o canplayer.c
arm-linux-gnueabihf-gcc   canplayer.o lib.o   -o canplayer
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o cangen.o cangen.c
arm-linux-gnueabihf-gcc   cangen.o lib.o   -o cangen
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o canbusload.o canbusload.c
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o canframelen.o canframelen.c
arm-linux-gnueabihf-gcc   canbusload.o canframelen.o   -o canbusload
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o log2long.o log2long.c
arm-linux-gnueabihf-gcc   log2long.o lib.o   -o log2long
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o log2asc.o log2asc.c
arm-linux-gnueabihf-gcc   log2asc.o lib.o   -o log2asc
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o asc2log.o asc2log.c
arm-linux-gnueabihf-gcc   asc2log.o lib.o   -o asc2log
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN  -c -o canlogserver.o canlogserver.c
arm-linux-gnueabihf-gcc   canlogserver.o lib.o   -o canlogserver
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   bcmserver.c   -o bcmserver
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotpdump.c   -o isotpdump
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotprecv.c   -o isotprecv
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotpsend.c   -o isotpsend
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotpsniffer.c   -o isotpsniffer
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotptun.c   -o isotptun
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotpserver.c   -o isotpserver
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   isotpperf.c   -o isotpperf
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   cangw.c -o cangw
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   slcan_attach.c   -o slcan_attach
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   slcand.c   -o slcand
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   slcanpty.c   -o slcanpty
arm-linux-gnueabihf-gcc -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   canfdtest.c   -o canfdtest

C:\VS2015\can-utils>
```
=> イケたね？！

- 3 インクルードPATHを確認。
上の、コンパイルしているとこどこか一行を `--verbose`をつけて実行

```
C:\VS2015\can-utils>arm-linux-gnueabihf-gcc  --verbose   -O2 -Wall -Wno-parentheses -fno-strict-aliasing -Iinclude -D_FILE_OFFSET_BITS=64 -DSO_RXQ_OVFL=40 -DPF_CAN=29 -DAF_CAN=PF_CAN   can-calc-bit-timing.c   -o can-calc-bit-timing

Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/4.9/lto-wrapper.exe
Target: arm-linux-gnueabihf
Configured with: ../gcc-4.9.2/src/configure --enable-win32-registry=SysGCC-arm-linux-gnueabihf-4.9.2 --prefix /q/gnu/linux/raspberry-jessie/out/ --disable-nls --without-libiconv-prefix --with-sysroot=/q/gnu/linux/raspberry-jessie/out/arm-linux-gnueabihf/sysroot --enable-languages=c,c++ --enable-shared --enable-linker-build-id --without-included-gettext --enable-threads=posix --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --disable-libquadmath --with-arch-directory=arm --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --target arm-linux-gnueabihf
Thread model: posix
gcc version 4.9.2 (GCC) 
COLLECT_GCC_OPTIONS='-O2' '-Wall' '-Wno-parentheses' '-fno-strict-aliasing' '-I' 'include' '-D' '_FILE_OFFSET_BITS=64' '-D' 'SO_RXQ_OVFL=40' '-D' 'PF_CAN=29' '-D' 'AF_CAN=PF_CAN' '-o' 'can-calc-bit-timing' '-v' '-march=armv6' '-mfloat-abi=hard' '-mfpu=vfp' '-mtls-dialect=gnu'
 c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/4.9/cc1.exe -quiet -v -I include -imultilib . -imultiarch arm-linux-gnueabihf -iprefix c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/ -isysroot c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot -D _FILE_OFFSET_BITS=64 -D SO_RXQ_OVFL=40 -D PF_CAN=29 -D AF_CAN=PF_CAN can-calc-bit-timing.c -quiet -dumpbase can-calc-bit-timing.c -march=armv6 -mfloat-abi=hard -mfpu=vfp -mtls-dialect=gnu -auxbase can-calc-bit-timing -O2 -Wall -Wno-parentheses -version -fno-strict-aliasing -o C:\Users\User01\AppData\Local\Temp\ccViaIpW.s
GNU C (GCC) version 4.9.2 (arm-linux-gnueabihf)
	compiled by GNU C version 4.8.1, GMP version 4.2.4, MPFR version 2.4.1, MPC version 0.8
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/sysgcc/raspberry/lib/gcc/../../lib/gcc/arm-linux-gnueabihf/4.9/include"
ignoring nonexistent directory "c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot/usr/include/arm-linux-gnueabihf/arm-linux-gnueabihf"
ignoring duplicate directory "c:/sysgcc/raspberry/lib/gcc/../../lib/gcc/arm-linux-gnueabihf/4.9/include-fixed"
ignoring duplicate directory "c:/sysgcc/raspberry/lib/gcc/../../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/include"
ignoring duplicate directory "c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot/usr/include/arm-linux-gnueabihf"
#include "..." search starts here:
#include <...> search starts here:
 include
 c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/include
 c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/include-fixed
 c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/include
 c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot/usr/include/arm-linux-gnueabihf
 c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot/usr/include
End of search list.
GNU C (GCC) version 4.9.2 (arm-linux-gnueabihf)
	compiled by GNU C version 4.8.1, GMP version 4.2.4, MPFR version 2.4.1, MPC version 0.8
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: e8d197b567eeb3ddfdef3de0808160ec
COLLECT_GCC_OPTIONS='-O2' '-Wall' '-Wno-parentheses' '-fno-strict-aliasing' '-I' 'include' '-D' '_FILE_OFFSET_BITS=64' '-D' 'SO_RXQ_OVFL=40' '-D' 'PF_CAN=29' '-D' 'AF_CAN=PF_CAN' '-o' 'can-calc-bit-timing' '-v' '-march=armv6' '-mfloat-abi=hard' '-mfpu=vfp' '-mtls-dialect=gnu'
 c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/bin/as.exe -v -I include -march=armv6 -mfloat-abi=hard -mfpu=vfp -meabi=5 -o C:\Users\User01\AppData\Local\Temp\cc4dZWgV.o C:\Users\User01\AppData\Local\Temp\ccViaIpW.s
GNU assembler version 2.25 (arm-linux-gnueabihf) using BFD version (GNU Binutils) 2.25
COMPILER_PATH=c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/4.9/;c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/;c:/sysgcc/raspberry/bin/../libexec/gcc/;c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/bin/
LIBRARY_PATH=c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/;c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/;c:/sysgcc/raspberry/bin/../lib/gcc/;c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/lib/;c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/lib/arm-linux-gnueabihf/;c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/lib/;c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib/arm-linux-gnueabihf/;c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib/
COLLECT_GCC_OPTIONS='-O2' '-Wall' '-Wno-parentheses' '-fno-strict-aliasing' '-I' 'include' '-D' '_FILE_OFFSET_BITS=64' '-D' 'SO_RXQ_OVFL=40' '-D' 'PF_CAN=29' '-D' 'AF_CAN=PF_CAN' '-o' 'can-calc-bit-timing' '-v' '-march=armv6' '-mfloat-abi=hard' '-mfpu=vfp' '-mtls-dialect=gnu'
 c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/4.9/collect2.exe -plugin c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/4.9/liblto_plugin-0.dll -plugin-opt=c:/sysgcc/raspberry/bin/../libexec/gcc/arm-linux-gnueabihf/4.9/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\User01\AppData\Local\Temp\ccVNv1pU.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot --build-id --eh-frame-hdr -dynamic-linker /lib/ld-linux-armhf.so.3 -X --hash-style=gnu -m armelf_linux_eabi -o can-calc-bit-timing c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib/arm-linux-gnueabihf/crt1.o c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib/arm-linux-gnueabihf/crti.o c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/lib/crtbegin.o -Lc:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9 -Lc:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf -Lc:/sysgcc/raspberry/bin/../lib/gcc -Lc:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/lib -Lc:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/lib/arm-linux-gnueabihf -Lc:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/lib -Lc:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib/arm-linux-gnueabihf -Lc:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib C:\Users\User01\AppData\Local\Temp\cc4dZWgV.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed c:/sysgcc/raspberry/bin/../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/lib/crtend.o c:/sysgcc/raspberry/bin/../arm-linux-gnueabihf/sysroot/usr/lib/arm-linux-gnueabihf/crtn.o
```
 - この辺がincludeしてるあたりっぽいので、あとでVSの設定にいれる。

```
#include <...> search starts here:
 include
 c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/include
 c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/include-fixed
 c:\sysgcc\raspberry\bin\../lib/gcc/arm-linux-gnueabihf/4.9/../../../../arm-linux-gnueabihf/include
 c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot/usr/include/arm-linux-gnueabihf
 c:\sysgcc\raspberry\bin\../arm-linux-gnueabihf/sysroot/usr/include
End of search list.
```


- 4 Visual Studio 2015にて
 - プロジェクト作成: ↑と同様にFrom Existing Code...

```
Project File Location: C:\VS2015\can-utils
Project name: can-utils
---
[x] Use external build system
---
build.bat`
build.bat clean && build.bat
build.bat clean
で `[Finish]
```
- Include Pathの追加
 - Solution Explorerで can-utilsで右クリック => VC++ DirectoriesでInclude Directoriesを変更して、`[OK]`

```
C:\VS2015\can-utils\include;C:\SysGCC\Raspberry\lib\gcc\arm-linux-gnueabihf\4.9\include;C:\SysGCC\Raspberry\lib\gcc\arm-linux-gnueabihf\4.9\include-fixed;C:\SysGCC\Raspberry\arm-linux-gnueabihf\include;C:\SysGCC\Raspberry\arm-linux-gnueabihf\sysroot\usr\include\arm-linux-gnueabihf;C:\SysGCC\Raspberry\arm-linux-gnueabihf\sysroot\usr\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);
```

- `Makefile`修正
 - `-O2`を `-O0`最適化なしに、`-g`もつける。
`CFLAGS    = -O0 -g -Wall -Wno-parentheses \ `
 - `all:` に`copy_to_rasp`追加
`all: $(PROGRAMS) copy_to_rasp`
 - `copy_to_rasp`も同様なのを作る。

```tex:Makefileの一番下に追加。
PUTTY_SCP=C:\Tools\putty\pscp.exe
PUTTY_PLINK=C:\Tools\putty\plink.exe
SSH_KEY=C:\Users\User01\.ssh\id_rsa-20151002.ppk

RASP_IP=raspberrypi
RASP_WORKDIR=/home/pi/dev/can-utils-exe
copy_to_rasp: $(PROGRAMS)
	$(PUTTY_PLINK) -i $(SSH_KEY) pi@$(RASP_IP) mkdir -p $(RASP_WORKDIR)
	$(PUTTY_SCP)   -i $(SSH_KEY) $(PROGRAMS) pi@$(RASP_IP):$(RASP_WORKDIR)
	$(PUTTY_PLINK) -i $(SSH_KEY) pi@$(RASP_IP) chmod 755 $(RASP_WORKDIR)/*
	$(PUTTY_PLINK) -i $(SSH_KEY) pi@$(RASP_IP)     ls -l $(RASP_WORKDIR)
```

- 5 リビルド: メニューから、`Build > Rebuild Solution` 
 => 実行ファイルたちが、`/home/pi/dev/cam-utils-exe/`フォルダにコピーされる。
 
 ```
 1>  C:\Tools\putty\plink.exe -i C:\Users\User01\.ssh\id_rsa-20151002.ppk pi@raspberrypi     ls -l /home/pi/dev/can-utils-exe
1>  total 636
1>  -rwxr-xr-x 1 pi pi 32264 Oct 13 14:44 asc2log
1>  -rwxr-xr-x 1 pi pi 16816 Oct 13 14:44 bcmserver
1>  -rwxr-xr-x 1 pi pi 27692 Oct 13 14:44 canbusload
1>  -rwxr-xr-x 1 pi pi 21464 Oct 13 14:44 can-calc-bit-timing
1>  -rwxr-xr-x 1 pi pi 47180 Oct 13 14:44 candump
1>  -rwxr-xr-x 1 pi pi 18532 Oct 13 14:44 canfdtest
1>  -rwxr-xr-x 1 pi pi 38480 Oct 13 14:44 cangen
1>  -rwxr-xr-x 1 pi pi 29400 Oct 13 14:44 cangw
1>  -rwxr-xr-x 1 pi pi 38644 Oct 13 14:44 canlogserver
1>  -rwxr-xr-x 1 pi pi 40640 Oct 13 14:44 canplayer
1>  -rwxr-xr-x 1 pi pi 28516 Oct 13 14:44 cansend
1>  -rwxr-xr-x 1 pi pi 30552 Oct 13 14:44 cansniffer
1>  -rwxr-xr-x 1 pi pi 18228 Oct 13 14:44 isotpdump
1>  -rwxr-xr-x 1 pi pi 18516 Oct 13 14:44 isotpperf
1>  -rwxr-xr-x 1 pi pi 15820 Oct 13 14:44 isotprecv
1>  -rwxr-xr-x 1 pi pi 15808 Oct 13 14:44 isotpsend
1>  -rwxr-xr-x 1 pi pi 22152 Oct 13 14:44 isotpserver
1>  -rwxr-xr-x 1 pi pi 18780 Oct 13 14:44 isotpsniffer
1>  -rwxr-xr-x 1 pi pi 19680 Oct 13 14:44 isotptun
1>  -rwxr-xr-x 1 pi pi 29220 Oct 13 14:44 log2asc
1>  -rwxr-xr-x 1 pi pi 25048 Oct 13 14:44 log2long
1>  -rwxr-xr-x 1 pi pi 14308 Oct 13 14:44 slcan_attach
1>  -rwxr-xr-x 1 pi pi 19360 Oct 13 14:44 slcand
1>  -rwxr-xr-x 1 pi pi 19936 Oct 13 14:44 slcanpty
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
```

### candumpをデバッグ
- 1 LaunchOption_candump.xmlを作成
`ExeArguments`に`can0`を入れてます。

```xml:LaunchOption_candump.xml
<PipeLaunchOptions xmlns="http://schemas.microsoft.com/vstudio/MDDDebuggerOptions/2014"
    PipePath="C:\Tools\putty\plink.exe"
    PipeArguments="-i C:\Users\User01\.ssh\id_rsa-20151002.ppk pi@raspberrypi -batch -t gdb --interpreter=mi"
    ExePath="/home/pi/dev/can-utils-exe/candump"
    ExeArguments="can0"
    TargetArchitecture="X64"
    WorkingDirectory="/home/pi/dev/can-utils-exe"
    AdditionalSOLibSearchPath="">
</PipeLaunchOptions>
```

- 2 RaspberryPi側で、SocketCANが使えるようにしておく

```shell:[Raspi-Console]
sudo ip link set can0 type can bitrate 250000
sudo ip link set can0 up
```

- 3 デバッグ
 - candump.cをひらいて、ブレークポイントをはる
	`617:  while (running) {` あたりがメインループっぽい。
![6.png](https://qiita-image-store.s3.amazonaws.com/0/75594/ab4d61cd-cfe7-2066-15f2-bc40a48efa05.png)
 - Command Windowを開く(`CTRL+ALT+A`)

```text:
Debug.MIDebugLaunch /Executable:HelloWorld /OptionsFile:C:\VS2015\can-utils\LaunchOption_candump.xml
```

 - あとは、デバッグ。`struct timeval`構造体もみれてるね
![9.png](https://qiita-image-store.s3.amazonaws.com/0/75594/8162e7a8-f8d8-da16-6dd3-143090ddd50c.png)