使用VS2008下調試SGI STL源碼

轉自:http://www.tuicool.com/articles/j2ER3y

 

相信 很多C++的程序員都看過侯捷先生寫的《STL源碼剖析》,即使沒看過,也有所聞。正如侯先生開篇所說:源碼之前了無祕密。但有很多人只是看了這本書,沒有實踐跟進去了解具體的運行機理,沒有實踐的理論,總是不那麼深刻的。本文一步一步教你在本機上把SGI STL編譯並調試。廢話少說,開始。

0. 前置條件:windowsXP或win7(我本機是win7,但說明裏明顯是說支持xp的),已經安裝VS2008(其他版本應該也可以,但我沒試過),有一定C++基礎,最好看過侯捷先生的《STL源碼剖析》

1. 用VS2008新建一個控制檯程序,解決方案的名字不重要,你可以隨便起;我的工程名字叫SGI_STL,工程目錄爲SlnDir/SGI_STL。你先編譯運行一下,如果沒問題就下一步。注:以下所有SlnDir都要替換成你解決方案的目錄名字。

2. 下載SGI STL源代碼 http://sourceforge.net/projects/stlport/ ;上面的名字是叫STLport,我使用的版本是 STLport-5.2.1。下載到本地然後解壓到目錄:SlnDir/SGI_STL/STLport-5.2.1/。我們的SlnDir/SGI_STL/目錄下面就有了這些文件:

3. 我們進去STLport-5.2.1目錄下看到下列文件(我這部分內存是直接貼了README的內容出來),看的懂就看,不懂就數數是不是這麼多文件就行了。

This directory contains the STLport-5.0 release.

What's inside :

README           - this file
INSTALL          - installation instructions

bin              - installation directory for STLport unit tests;
                   it may contain more subdirs, if you use
                   crosscompilation
build/lib        - build directory for STLport library (if you use
                   STLport iostreams and/or locale only)
build/test/unit  - build directory for regression (unit) tests
build/test/eh    - build directory for exception handling tests
stlport          - main STLport include directory
src              - source for iostreams implementation and other parts
                   that aren't pure template code
lib              - installation directory for STLport library (if you
                   use STLport iostreams and/or locale only);
                   it may contain more subdirs, if you use
                   crosscompilation
test/unit        - unit (regression) tests
test/eh          - exception handling test using STLport iostreams
etc              - miscellanous files (ChangeLog, TODO, scripts, etc.)

4. 從啓動程序裏啓動VS2008的命令行提示,用CD命令轉到SlnDir/SGI_STL/STLport-5.2.1/目錄下,輸入:configure msvc9 正常的話出來下面的結果,但注意系統版本顯示錯誤了,我的是win7但他顯示了winxp,不管,反正能通過。

5. 看到上面給出的提示了嗎?沒錯,進入到build/lib目錄下,輸入nmake clean install,後面就等幾分鐘編譯過程吧。

6. 編譯完畢,進入到SlnDir/SGI_STL/STLport-5.2.1/bin目錄下,應該有三個pdb文件和三個dll文件,把這六個文件複製到SlnDir/SGI_STL/Debug目錄下面(當然你設置的輸出目錄不是這裏,你當然也應該明白放到哪裏合適)。

7. 現在還差兩步了,現在,在VS2008界面打開Tools/Option如下面的界面,設置VC++ Directories,在Show directories for選擇Executable files. 新增加一項內容爲SlnDir/SGI_STL\STLport-5.2.1\bin 並放到最上面; 選擇Include files新增加一項SlnDir\SGI_STL\STLport-5.2.1\stlport並放到最上面;選擇Library files,新增加一項內容爲SlnDir\SGI_STL\STLport-5.2.1\lib;點擊OK。

8. 最後一步了,在你剛纔新建的工程裏面,打開SGI_STL.cpp 代碼如下:如果編譯運行沒問題,可以進行調試了。

#include "stdafx.h"
#include "STLport-5.2.1/stlport/vector"
int _tmain(int argc, _TCHAR* argv[])
{
  std::vector<int> v;
  v.push_back(2);
  v.push_back(4);
  return 0;
}

好吧,可以單步調試進入代碼中看了,開始你的SGI STL源代碼之旅吧!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章