歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用Visual Studio 利用WinGDB編譯和遠程調試嵌入式Linux的程序

使用Visual Studio 利用WinGDB編譯和遠程調試嵌入式Linux的程序

日期:2017/3/1 9:20:05   编辑:Linux編程

寫這篇文章的目的在於幫助那些既要使用Visual Studio編寫程序又要開發和調試嵌入式Linux 程序的苦命程序員們!

第一步, 安裝 WinGDB ,下載位置 http://www.wingdb.com/ ,連接是http://www.wingdb.com/WinGDB-latest_trial.msi 30天試用版在30天後 卸載掉, 刪除所有gdb相關的注冊表, 然後重新安裝, 就又可以使用30天, 安裝過程如下圖示:

1、選擇同意許可協議

2、如果你機器上裝了足夠多的VS版本, 你都可以選擇, 主要看你用那個版本的VS

3、如果你僅作遠程Linux開發 , 則選第一項, 如果你要做嵌入式系統開發, 選擇第二項, 如果你想全部要, 則選第三項 , 我們這裡當然是選擇第三項了。

第二步、創建WinGDB項目

1、新建WinGDB項目,選擇Multiplatform executable

2、選擇第一項, 我們要進行遠程開發,源碼在構建在遠程服務器上, 調試也同樣在遠程主機上, 通過TCP/IP連接並使用SSH協議來訪問這些機器。

3、選擇第二項, gdb 和交叉編譯環境在一台服務器上, 遠程調試在另外一台機器上, 這台機器可以是arm linux 也可以是其他支持gdbserver 的機器。

4、填寫gdb和編譯環境的ssh地址和用戶名密碼 以及 gdbserver 所在的用戶名密碼和IP地址。

5、惦記Browse 選擇我們事先編譯好的 gdb 調試器,這是運行在編譯環境那台服務器上的gdb , 用來控制 gdbserver 的 , gdb和gdbserver必須是對應的。

6、選擇本地源碼和遠程的源碼的同步方式, 這個根據情況來選擇, 如果你是共享的, 擇選第一項, 如果需要同步, 則選第二項, 如果不需要同步, 則選第三項, 如圖:

7、選擇遠程文件路徑和本地文件路徑:

8、設置編譯 命令, 和編譯目錄, 這些目錄在點擊browse時都是列出的編譯服務器上的目錄

9、選擇遠程調試的目標目錄 , 如圖:

10、選擇額外和附加的源碼文件和庫 ,這步直接跳過, 如果你有需要, 則可以再這裡設置。

11、設置調試啟動參數 ,與此同時, 建議把Stop in main 去掉, 這樣避免每次啟動程序時, 都要在main 函數哪裡停下來。 如圖:

12、配置完成, 如圖:

我的配置內容如下:

Project build command: make
Project rebuild command: make clean; make
Project clean command: make clean
Project deploy command:
Build working directory: /root/ct/src/
Build environment variables: BUILD_CONFIGURATION="Debug"
Build before start debugging: Build project
Deploy before start debugging: yes
Deploy action: Transfer file to target
Deploy only if file changed: yes
Files to deploy: /root/ct/src//EMRCV4
Deploy target directory: /mnt/EMRCV4/BIN
Core file path:
Working directory: /mnt/EMRCV4/BIN
Arguments:
Environment variables:
Stop in main(): no
Target specification:
Server mode: Launch executable
Executable on target: /mnt/EMRCV4/BIN/EMRCV4
Launch GDB server automatically: yes
Launch server from sysroot: no
Path to gdbserver: /usr/bin/gdbserver
Server port: 2345
Server port is forwarded: no
Forwarded server port: 0
Executable path: /root/ct/src//EMRCV4
Byte order: Little endian
Initialization script:
Additional source directories:
Debug info directory:
Sysroot on host:
Sysroot on target:
Shared library directories:
Visualizer profile:
Load sources from build host: no
Disable hang reporter: no
Debugger path: /usr/bin/arm-unknown-linux-gnueabi-gdb
Build toolchain paths:
Intercept standard IDE commands: yes
Target type: Linux indirect debugging (SSH+remote target)
Debug host login: [email protected]:22
Build host login: [email protected]:22
Debug target login: [email protected]:22

至此完成項目的調試和編譯配置

第三部、代碼映射配置

此時輸出窗口中會輸出如下內容:

-------------------------------------------------------------------------------- Transfer to remote host(s) --------------------------------------------------------------------------------

Errors were encountered during preparation: No files could be copied.

Could not map the path: D:\Works\RHY\Toll\Trunk\src\FunctionAssembly\Lane\EMRCV4\EMRCV4.vcxproj to remote filesystem. Use 'Map source directories' option to setup a mapping.

如果出現, 則打開代碼映射窗口

然後在下面添加一個Login , 再選擇添加一個映射, 如圖:

下面是編譯時的效果, 由於編譯器錯誤等級原因, 第一次編譯會有很多錯, 第二次編譯則錯誤消失 ,

如圖:

調試時 , 啟動時會彈出下面的窗口, 點確定即可, 不用理會

下面是調試過程的情況, 可以查看運行時當前進程的局部變量和鼠標下面的變量值, 這個跟VS一樣, 也可以單步執行。

第四步:頭文件引用 , 上圖中你可以看到很多常量和函數都有紅線, 是因為VS找不到對應的頭文件, 因此需要繼續做些設置。

打開項目屬性, 選中配置屬性, 選中VC++目錄,然後在包含目錄中加入 交叉編譯環境中的頭文件,比如:

D:\Works\Linux\toolchain_R2_EABI\usr\arm-unknown-linux-gnueabi\sysroot\usr\include

然後, 你需要把你所有頭文件的目錄全部添加在這裡, 如果頭文件目錄比較多, 就比較費勁。

設置完後你會看到VS正在解析頭文件, 設置後效果如下:

最後, 如果向導設置有問題或者沒有保存, 可能是由於不是管理員權限的原因, 因此你可能需要檢查下你的配置,

方法如下圖:

常規設置 debug host 和 build host 一樣

環境中設置調試器路徑

調試選項中, 要選擇好生產的可執行文件 和 在 目標機器上的 應用程序工作目錄

調試目標

gdbserver 設置

下面一步很關鍵, 是項目生成設置, 生成目錄必須和路徑映射中的路徑一直, 否則報錯的錯誤無法定位文件位置,

第七欄中設置 調試前生成項目, 第八項設置調試前需要部署, 第十項設置 部署操作為傳送文件至目標,

第十一項選擇是, 當文件改變則部署, 第十二項決定要部署的文件, 第五項是 要部署的文件位置。

結束語

另外編譯arm 相關的gdbserver 和 gdb在以後的文章中也會介紹到!這篇文章就不做介紹了。 如果gdb和gdbserver 不一致, 則同樣會無法調試

下面是我的gdbserver 的版本信息:

[192.168.50.58:/mnt/EMRCV4/BIN]# gdbserver --version
GNU gdbserver (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
gdbserver is free software, covered by the GNU General Public License.
This gdbserver was configured as "arm-unknown-linux-gnueabi"
[192.168.50.58:/mnt/EMRCV4/BIN]#

下面是gdb的版本信息

root@Ubuntu:/usr/bin# arm-unknown-linux-gnueabi-gdb
GNU gdb (GDB) 7.2
Copyright (C) 2010 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 "--host=i686-pc-linux-gnu --target=arm-unknown-linux-gnueabi".
For bug reporting instructions, please see:
< http://www.gnu.org/software/gdb/bugs/>.

注意, 一定要注意 This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-unknown-linux-gnueabi".

如果不一致, 同樣會編譯失敗的。

至此完全配置完畢,在稍後的幾天裡, 我會根據情況來補充和完善這篇文章, 如有興趣, 請繼續關注吧!

使用Visual Studio Code開發TypeScript http://www.linuxidc.com/Linux/2015-07/119456.htm

Visual Studio Code 簡單試用體驗 http://www.linuxidc.com/Linux/2015-05/116887.htm

Visual Studio Code試用體驗 http://www.linuxidc.com/Linux/2015-07/120378.htm

Visual Studio 2010 & Help Library Manager 安裝說明 http://www.linuxidc.com/Linux/2012-11/74814.htm

OpenCV 2.3.x/2.4.x在Visual Studio 2005/2008和Visual Studio 2010配置方法詳解 http://www.linuxidc.com/Linux/2012-08/68302.htm

使用OpenCV-2.4.0.exe文件編譯x86或x64平台Visual Studio 2005/2008/2010目標文件 http://www.linuxidc.com/Linux/2012-08/68305.htm

Visual Studio LightSwitch增加對HTML5和JavaScript的支持 http://www.linuxidc.com/Linux/2012-06/63397.htm

Visual Studio 11:使用 C++ 開發一個最簡單的 Metro 應用 http://www.linuxidc.com/Linux/2012-06/62657.htm

Visual Studio 的詳細介紹:請點這裡
Visual Studio 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved