LoginSignup
27
28

More than 5 years have passed since last update.

Visual Studio 2015で、LinuxのHelloWorldデバッグ(MIDebugEngine使用)

Last updated at Posted at 2015-10-03

MIEngineを使って、Visual Studio 2015から、Linuxのgdbに接続して、VSのIDEのユーザインターフェースで、デバッグ。

  • ラズパイでやってみた記事はこちら VS2015でRaspberryPiデバッグ
  • mingw/cygwinのgcc/gdbで、ローカルでもできました(追記10/6:おまけ)
  • gdbとなんかやってるぽいですね gdb2.png

下準備

  1. Windows
  2. Linux環境
    • openssh-server
    • 開発環境: gccとかgdbとか、(build-essential)入れとく。
    • sambaいれて、Windowsのネットワークドライブにマウントできるようにしておく。=> 今回は linuxのユーザホーム~J:\ に マウントしました。
  • 上でわからないことは、 ググるか、コメントでたずねると、詳しく知ってる、親切な人が... きっと...

Windowsから、Plink.exeにて接続確認

  • ↓をためして、Hello World!がかえってくると、OK.
C:\Tools\PuTTY\PLINK.EXE -i putty_private_key.ppk user01@linux_ip_address echo Hello World!
Hello World!

ファイルの準備

/home/user01/HelloWorld/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;
}

VSで普通にビルドして、うごかみる。

  1. VS2015起動
  2. File > New > Project From Existing Code...から
    • Visual C++ [Next]
    • Project file location: J:\HelloWorld
    • Project name: HelloWorld
    • [Next] 3.とりあえず..
    • Use Visual Studio で、
    • [Console application project]
    • [Next]
  3. [Next]
  4. [Finish]
  5. HelloWorld.cppを開いて、適当なとこに、Breakpointおいて、[F5]おして、通常動作するか確認。
  • Security Alertがでたら、[ ] チェックをはずして [OK]

Linux 上でビルド

  • 1. ファイル確認。ls
linux-console
user01@hostname:~/HelloWorld$ cd ~/HelloWorld/
user01@hostname:~/HelloWorld$ ls -1
Debug
HelloWorld.cpp
HelloWorld.sln
HelloWorld.vcxproj
HelloWorld.vcxproj.filters
LaunchOptionsLinux.xml
user01@hostname:~/HelloWorld$
  • 2. ビルドして、実行

user01@hostname:~/HelloWorld$ gcc -o HelloWorld HelloWorld.cpp -g -O0
user01@hostname:~/HelloWorld$ ls -1
Debug
HelloWorld
HelloWorld.cpp
HelloWorld.sln
HelloWorld.vcxproj
HelloWorld.vcxproj.filters
LaunchOptionsLinux.xml
user01@hostname:~/HelloWorld$ ./HelloWorld
Hello World 9
user01@hostname:~/HelloWorld$

VSからデバック

MIDebugEngineへのオプションファイルを準備

  • Plinkへのパスとか、
  • ppkへのパスとか、
  • linux host addressとか、
  • linuxユーザ名とか うまく編集してください。
/home/user01/HelloWorld/LaunchOptionLinux.xml
<PipeLaunchOptions xmlns="http://schemas.microsoft.com/vstudio/MDDDebuggerOptions/2014"
    PipePath="C:\Tools\PuTTY\PLINK.EXE"
    PipeArguments="-i C:\Users\User01\.ssh\putty_private_key.ppk user01@linux_ip_address -batch -t gdb --interpreter=mi"
    ExePath="/home/user01/HelloWorld/HelloWorld"
    ExeArguments=""
    TargetArchitecture="X64"
    WorkingDirectory="/home/user01/HelloWorld"
    AdditionalSOLibSearchPath="">
</PipeLaunchOptions>

Win:VS2015から

  1. HelloWorld.cppをひらいて、ブレークポイントをはる。
  2. View > Other Windows > Command Window (Ctrl+Alt+A)でコマンドウインドウを開く(下)
  3. Command WindowにDebug.MIDebugLaunch /Executable:HelloWorld /OptionsFile:J:\HelloWorld\LaunchOptionsLinux.xml といれて... gdb1.png
  4. [Enter]をおす gdb2.png
  5. Visual Studio 2015のUIで、デバッガがうごきます。
    • [F10] Step Over
    • [F11] Step Into
    • Shift+[F11] Step Out
    • 変数の値を変更したり、Call Stackみたりできます。

  • main関数のあたまにbreakpointはってもとまらない???
  • CommandWindowからでなく、[F5]から、いけるといいんだが...
  • VSで編集 => Linux側でのBuild => VSからデバッグ ... が簡単にできると...
  • RasPiをホストにして、つないでみるのもよいか、と。(次回のネタ?) 書きました => VS2015でRaspberryPiデバッグ

おまけ:mingwとかcygwinのgccとgdb使って、ローカルWindowsで同様にデバッグできるようです。

LaunchOptionMinGW.xml
<PipeLaunchOptions xmlns="http://schemas.microsoft.com/vstudio/MDDDebuggerOptions/2014"  
    PipePath="C:\Qt\Tools\mingw491_32\bin\gdb.exe"
    PipeArguments=" --interpreter=mi"  
    ExePath="C:\VS2015\Projects\hello-mingw\HelloWorld.exe"
    ExeArguments=""  
    TargetArchitecture="X86"
    WorkingDirectory="C:\VS2015\Projects\hello-mingw"
    AdditionalSOLibSearchPath="">  
</PipeLaunchOptions>

gdb3.png

27
28
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
27
28